Inconsistent NSDateFormatter result for identical string format
Why does this conversion work some of the time and not others? When it doesn't work, eDate is null. The original conversion fails.. yet the formats between a working & a non-working date string look identical. I tried adding an extra "d" in both formatters for dd instead of d. That didn't fix the issue
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]init]autorelease];
[dateFormatter setDateFormat:@"EEE MMM d hh:mm:ss Z yyyy"];
NSDate *eDate = [dateFormatter dateFromString:[responceDictionary objectForKey: @"created_at"]];
NSLog(@"original string: %@", [responceDictionary objectForKey: @"created_at"]);
NSDateFormatter *shortFormatter = [[[NSDateFormatter alloc] init] autorelease];
[shortFormatter setDateFormat:@"EEE MMM d hh:mm"];
NSString *todayString = [shortFormatter stringFromDate:eDate];
NSLog(@"todayString: %@", todayString);
2011-05-12 11:01:52.768 Application[1707:207] original string: Wed May 11 19:30:57 +0000 2011
2011-05-12 11:01:52.769 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.770 Application[1707:207] original string: Tue May 10 18:45:01 +0000 2011
2011-05-12 11:01:52.770 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.771 Application[1707:207] original string: Tue May 10 02:24:48 +0000 2011
2011-05-12 11:01:52.771 Application[1707:207] todayString: Mon May 9 09:24
2011-05-12 11:01:52.772 Application[1707:207] original string: Tue May 10 01:05:05 +0000 2011
2011-05-12 11:01:52.772 Application[1707:207] todayString: Mon May 9 08:05
2011-05-12 11:01:52.773 Application[1707:207] original string: Mon May 09 17:38:14 +0000 2011
2011-05-12 11:01:52.774 Application[1707:207] todayString: (null)
2011-05-12 11:01:52.774 Application[1707:207] original string: Fri May 06 12:36:30 +0000 2011
2011-05-12 11:01:52.775 Applic开发者_C百科ation[1707:207] todayString: Thu May 5 07:36
2011-05-12 11:01:52.775 Application[1707:207] original string: Fri May 06 12:08:58 +0000 2011
2011-05-12 11:01:52.776 Application[1707:207] todayString: Thu May 5 07:08
2011-05-12 11:01:52.777 Application[1707:207] original string: Fri May 06 11:56:44 +0000 2011
2011-05-12 11:01:52.778 Application[1707:207] todayString: Fri May 6 06:56
2011-05-12 11:01:52.778 Application[1707:207] original string: Thu May 05 22:17:48 +0000 2011
2011-05-12 11:01:52.779 Application[1707:207] todayString: (null)
try HH
instead of hh
in your dateformat.
The unicode date format specification states that h
means hour in 12 (1-12) hour format and H
is the hour in 24 (0-23) hour format.
The NSDateFormatter can't create a date because 22 (as in your last example) is beyond the limit of 12 hours.
精彩评论