Problem formatting date using NSDateFormatter - iphone

I have the following date:
2011-09-09T18:01:47Z
I want it to appear as
"9/9/11 at 1:47PM" whatever the correct time is
This code returns a null string:
NSLog(#"TIME: %#",[message valueForKey:#"created_at"]);
NSString* time = [message valueForKey:#"created_at"];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM-dd-yy at HH:mm:ss"];
NSDate* newTime = [dateFormatter dateFromString:time];
[dateFormatter setDateFormat:#"MM-dd-yy at HH:mm:ss"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];

Your formatting is the same for both and they have little to no relation to your input and your output. Refer to the Unicode Data Format Patterns whenever you have issues.
NSString *datein = #"2011-09-09T18:01:47Z";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
//The Z at the end of your string represents Zulu which is UTC
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:#"UTC"]];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate* newTime = [dateFormatter dateFromString:datein];
//Add the following line to display the time in the local time zone
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setDateFormat:#"M/d/yy 'at' h:mma"];
NSString* finalTime = [dateFormatter stringFromDate:newTime];
NSLog(#"%#", finalTime);

Related

Get time from NSDate returns nil

I am trying to show time on graph and i have a full time stamp in this #"2012-08-28 18:50:24" format. when i try to get time from this date then it returns nil in NSDate.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
in this code only if I change [formatter setDateFormat:#"HH:mm:ss"]; line to [formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; then it starts working with full date and time. But i need only time
u have string . so you must have to convert it into date first as per your string format. after that you can convert date into any format. so follow this.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *str = [formatter stringfromdate:time];
NSLog(#"%#",str);
NSDateFormatter is used to format your NSDate to your requirements.
Here you are fetching the date from a string so you have to tell the NSDateFormatter to look for the string with that format and convert it into date
NSDate contains both date and time so if you want the time only to show use dateformatter to get the time component from NSDate
iOS manages time and date together in NSDate.Actually they are supposed to work together.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
// GET Proper NSDate
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
//To your requirement
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timestr=[formatter stringFromDate:time];
NSLog(#"%#",timestr);
Because, first you need to convert your string into NSDate in the right formate that is
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *aDate = [formatter dateFromString:#"2012-08-28 18:50:24"];
Now you can reformate the NSDateFormatter according to your need. Now you can get time from the
[formatter setDateFormat:#"HH:mm:ss"];
NSString *newDate = [dateFormatter stringFromDate:aDate];
If you need to produce Date from String then you'll need to set the date format accordingly.
This is important that you create a NSDate object first! Only then you can separate time from whole date.
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *time = [formatter dateFromString:#"2012-08-28 18:50:24"];
then
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:time];
You can also use other string concatenation functions to remove the Date Modules like this.
However, if you have an NSDate first, then your previous code would work.
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"HH:mm:ss"];
NSString *timeStr = [formatter stringFromDate:date];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"hh:mm:ss a"];
NSString *theTime = [timeFormat stringFromDate:[NSDate date]];
NSLog(#"time, %#",theTime);
First you have converted your string date and time in the date formate..
YourTimeStr = #"2012-08-28 18:50:24";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-mm-dd HH:mm:ss";
NSDate *DateAndTime = [formatter dateFromString:LogOutTimeSTR];
then
NSDateFormatter *dateFormatter1 = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter1 setDateFormat:#"HH:mm:ss"];
[dateFormatter1 setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
NSString *YourTimeStr =[dateFormatter1 stringFromDate:DateAndTime];

NSDate from string

I have a string "2012-09-16 23:59:59 JST"
I want to convert this date string into NSDate.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd hh:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 JST"];
NSLog(#"%#", capturedStartDate);
But it is not working. Its giving null value. Please help..
When using 24 hour time, the hours specifier needs to be a capital H like this:
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
Check here for the correct specifiers : http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
However, you need to set the locale for the date formatter:
// Set the locale as needed in the formatter (this example uses Japanese)
[dateFormat setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ja_JP"]];
Full working code:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss zzz"];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"ja_JP"]];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 JST"];
NSLog(#"Captured Date %#", [capturedStartDate description]);
Outputs (In GMT):
Captured Date 2012-09-16 14:59:59 +0000
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss Z"];
NSDate *capturedStartDate = [dateFormatter dateFromString: #"2012-09-16 23:59:59 GMT-08:00"];
NSLog(#"%#", capturedStartDate);
NSString *dateString = #"01-02-2010";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// this is imporant - we set our input date format to match our input string
// if format doesn't match you'll get nil from your string, so be careful
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
// end
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];

NSDateFormatter how can I format this date?

I am attempting to format an NSString which looks like:
#"2012-07-19T02:58:33Z"
to an NSDate with the following format:
#"MMMM dd, yyyy HH:mm a"
The NSDateFormatter always returns nil and I think it is because it cannot convert the NSString in it's current format to the desired one. Here is my code:
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *date = [formatter dateFromString:dateString];
Do I need to store the date in a differant format? Can I make this conversion possible with the current NSString?
Thanks!
Your dateFormatter doesn't match your dateString... Try this
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *date = [formatter dateFromString:dateString];
[formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
dateString=[formatter stringFromDate:date];
now dateString contains "July 19, 2012 02:58 AM"
Hope this helps
Try it.
NSString *myDateString=#"2012-07-19T02:58:33Z";//#"2009-07-06T17:42:12";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *myDate = [[NSDate alloc] init];
myDate = [dateFormatter dateFromString:myDateString];
NSLog(#"MyDate is: %#", myDate);
Your problem has already been answered.
Store your string in a NSDate with the format of the original string:
NSString *originalDateString = #"2012-07-19T02:58:33Z";
NSDateFormatter *originalFormatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSLocale *enUSLocale = [[NSLocale alloc] initWithLocaleIdentifier:#"en_US"];
[formatter setLocale:enUSLocale];
NSDate *originalDate = [formatter dateFromString:originalDateString];
NSDateFormatter *newFormatter = [formatter setDateFormat:#"MMMM dd, yyyy HH:mm a"];
NSString *newDateString = [formatter stringFromDate:originalDate];

iPhone/OS X: Convert UTC format date&time value Calendar required format

I am struggling a bit to convert the UTC time to the date format what i want.
2012-03-10T09:30:00Z
needs to be converted to the below format
3:00:00pm April 19, 2012
Could you someone please help me to resolve this?
I tried like below,
NSString *formatted;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:#"HH:mm:ss EEEE, MMM d"];
// Here datetime= 2012-03-10T09:30:00Z
NSDate *date = [NSDate dateWithTimeIntervalSince1970: [datetime intValue]];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
But, its printing wrong date like "06:03:32 Thursday, Jan 1". Please correct me.
UPDATED: I am having another problem, its not giving me the correct time, but its giving the correct date though. My Code is below.
NSString *formatted;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:#"HH:mm:ss EEEE, MMM d"];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [df dateFromString:datetime];
NSString *dateString = [formatter stringFromDate:date];
NSLog(#"dateString: %#", dateString);
Thank you!
To convert an ISO date string to NSDate:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneWithName:#"UTC"]];
[df setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'Z'"];
NSDate *date = [df dateFromString:date_str];
You can then convert it to your local time zone:
[df setTimeZone:[NSTimeZone localTimeZone]];
[df setDateFormat:#"HH:mm:ss EEEE, MMM d"];
In your code, your [datetime intValue] is probably incorrect.

Manage date with NSDateFormatter

I try to get and manage date with NSDateFormatter but how to make it works.
I think it's a problem with my first "dateFormatter" which have not the right format, because when I use the second formatter with function "stringFromDate" and paramter "[NSDate date]" it works well, but not when I use my NSDate "dateFromString".
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:#"to-date"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateFormatter dateFromString:dateString];
[dateFormatter release];
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEE, MMM d, yyyy"];
//NSString *toDate = [dateFormatter stringFromDate:dateFromString];
NSString *toDate = [dateFormatter stringFromDate:[NSDate date]];
Thank you for your help :)
Well of course, the DateFormat is not the same as the one you are receiving from the server.
Try this:
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss Z"];
NSDate *date = [dateFormatter dateFromString:[dateString stringByReplacingOccurrencesOfString:#"Z" withString:#" +0000"]];
Assuming that to-date is a string and not already an NSDate try this instead:
//"dateString" have this format: "2011-05-26T16:18:26Z"
NSString *dateString = [[tracks objectAtIndex:0] objectForKey:#"to-date"];
NSDate *dateFromString = [[NSDate dateFromString:dateString];
If you want to display a date to the user formatted in a particular format you can then use set up an NSDateFormatter and use stringFromDate: to get a string to show the user. If it still doesn't display correctly set the calendar (I forget if you always have to set it or if there is a default).