开发者

What's the proper NSDateFormatter format for this string?

February, 26 2010 21:34:00

Based on all the documentation I can find, MMMM, 开发者_Go百科d yyyy H:m:s should be correct - but my NSDate dateFromString is returning null.


I once had lots of trouble with exactly this kind of problem until I explicitly set the formatter to en_US locale. systemLocale is NOT good because it may be something other than en_US, affecting e.g month/weekday names etc. This is a piece of working code:

NSDateFormatter *fmt = [[[NSDateFormatter alloc] init] autorelease];    
[fmt setDateFormat:@"eee MMM dd HH:mm:ss Z yyyy"];
[fmt setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];
NSDate *formattedDate = [fmt dateFromString:someStringContainingDate];


What region is the phone set to? If it's not en_US, you'll have to set the date formatter to it using setLocale.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜