HI all,
i have a date string like this
2011-03-31 13:32:02
i want to convert this date string to display like this
Thursday – March 31, 2011
Please help to solve this
Thanks in advance
Use NSDateFormatter. First initialize the formatter with the appropriate format to parse the source string, something like this:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Then use it to parse the date string:
NSDate *date = [formatter dateFromString:string];
Now you can create a new formatter to output in the new format, or just reuse the existing one:
[formatter setDateFormat:#"EEEE - MMMM dd, yyyy"];
NSString *output = [formatter stringFromDate:date];
Related
So I have this string date
"2017-07-25T11:02:00.000Z"
I want to format this date so that I can assign it to my date picker
Below is my code
//Set the date
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy"];
NSDate *dateFromString = [dateFormatter dateFromString:self.userDefaultQuotationModel.dateTime];
[self.quotationDatePicker setDate:dateFromString];
however, if I set the date format as #"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ", it gives me the correct result based on the format. But afterwards, I can't assign this date to the date picker.
I set the date format as #"MMM d, yyyy" because I am looking for the the nearest format as Date Picker's
my Date picker format is Jul 25 2017. I get the date format from this following website
http://nsdateformatter.com/
The *dateFromString does not contain any value after the dateFromString function, which is nil
I have been searching regarding this topic but couldn't find any. What am I missing here? thank you
NSString *strInputDateString = #"2017-07-25T11:02:00.000Z";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
//Set new dateFormate
NSDate *date1 = [dateFormat dateFromString:strInputDateString];
[dateFormat setDateFormat:#"MMM d, yyyy"];
NSString *strOutputDateString = [dateFormat stringFromDate:date1];
NSLog(#"%#",strInputDateString);
NSLog(#"%#",strOutputDateString);
I tried to convert String to date time and date time to string it's working but some what error. I not getting correct formate.
my excepting result should be:
like this.02/02/2013 05:24PM
time should be 12 hours formate;
NSString *dateTime = #"2013-02-02 15:54:31";
int time=[[temp objectForKey:#"created"]intValue];
NSLog(#"Time=%d",time);
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:dateTime]];
NSDate *date = [dateFormat dateFromString:dateTime];
NSLog(#"Nsdate=%#",date);
// Convert date object to desired output format
[dateFormat setDateFormat:#"hh:mma"];
dateTime = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(#"DateTime=%#",dateTime);
--------------------
NSLogs :
Time=2013
DateTime=2013-02-02 17:06:09
Nsdate=2013-01-02 11:30:00 +0000//it's not correct date
DateTime=02/01/2013 05:00PM//it's not correct date and time//
-------------
my excepting result should be like this:
DateTime :02/02/2013 05:24PM
If you have any idea please shear your ideas , It's will help me.
Change your this line
[dateFormat setDateFormat:#"YYYY-MM-DD HH:MM:SS"];
to
[dateFormat setDateFormat:#"YYYY-MM-dd HH:mm:SS"];
mm and dd should be small
Hope it helps you..
I'm trying to set NSDateFormatter for the below string:
2012-12-18T09:05:24.000Z
It's the output of S3Object Summary lastmodifed object. What is the format I have to set it to if I need to change the display format?Any idea?
NSString *stringe = #"2010-06-21T20:06:36+00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *reqDate = [dateFormatter dateFromString:string];
NSLog(#"%#",[dateFormatter stringFromDate:reqDate]);
Now you will get the date in NSDate object so you can change that date to as per your requirement.
For 2012-12-18T09:05:24.000Z
The format string is
yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
I have a string "2012-06-04" and am having a hard time converting it to: June 4, 2012.
Is there a quick way to transform this? I come from a ruby world where you would convert everything to seconds and that back out to the format you need it. Is there a reference that shows how to do that?
Thanks
One way to do so is to convert the string to an NSDate using the NSDateFormatter with the 2012-06-04 format, and then convert the NSDate back to a string using the June 4, 2012 format:
NSString* input = #"2012-06-04";
NSDateFormatter* df = [[NSDateFormatter alloc] init];
[df setDateFormat:#"yyyy-MM-dd"];
NSDate* date = [df dateFromString:input];
[df setDateFormat:#"MMMM d, yyyy"];
return [df stringFromDate:date];
The format string's syntax is described in UTS #35.
Something like
NSDateFormatter *fromDateFormatter = [[NSDateFormatter alloc] init];
fromDateFormatter.dateFormat = #"yyyy-MM-dd";
NSDate *date = [fromDateFormatter dateFromString:#"2012-06-04"];
NSLog(#"%#", date);
NSDateFormatter *toDateFormatter = [[NSDateFormatter alloc] init];
toDateFormatter.dateFormat = #"MMMM d, yyyy";
NSString *toDate = [toDateFormatter stringFromDate:date];
NSLog(#"%#", toDate);
#=> 2012-05-30 20:51:12.205 Untitled[1029:707] 2012-06-03 23:00:00 +0000
#=> 2012-05-30 20:51:12.206 Untitled[1029:707] June 4, 2012
To work this out you can use Apple's class references NSDateFormatter and other sources like this IPHONE NSDATEFORMATTER DATE FORMATTING TABLE and some trial and error
I am loading in dates from my web service, I'm sending dates in the format (GMT times): 02/11/11 10:56:09
I am creating an NSDate form this using NSDateFormatter as such:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
This works great, after I'm comparing this to the current date to get relative time intervals.
The problem is when the phone is set up in a different timezone, when I load in the date from my api, and use the date formatter, what seems to be happening is the phone is assuming the date string is local time and it's converting it to GMT.
Example:
I load in a date with the time 10am from the api.
The phone is set to PDT.
The date formatter is creating an NSDate assuming that my date string with 10am, is actually relevant to the phone.
I end up with a date and time equal to 5pm, adding 10 hours.
I am trying to specify in my date formatter that the string is GMT, but I'm having trouble, I've tried the following, adding GMT to the format:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss GMT"];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
This is not working.
Can anyone give any advice ?
Solution
Just a recap, I got it working with a terrible work around by appending GMT to the original string, and formatting that:
NSString * cheat = [NSString stringWithFormat:#"%# GMT", str];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss zzzz"];
NSDate *journeyDate = [dateFormatter dateFromString:cheat];
[dateFormatter release];
return journeyDate;
This was a kind of unstable hack, because if the string changed to include a timezone, it wouldn't work anymore. For anyone who needs to do as myself, the following is just a quick example on how to create an NSTimeZone.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd/MM/yyyy HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSDate *journeyDate = [dateFormatter dateFromString:str];
[dateFormatter release];
return journeyDate;
Thanks for the quick help.
I suspect you just want to use NSDateFormatter.setTimeZone to force it to use UTC. You don't want to change the format string because presumably the string doesn't include the letters "GMT" - instead, you want to change which time zone the string is interpreted in, which is what setTimeZone will do.
You should use the setTimeZone method: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html