开发者

Why does NSDateFormatter return nil?

Why is lDateFormatted nil on iPhone simulator 3.1.2 after executing this piece of code?

NSString *lDateString = @"Wed, 17 Feb 2010 16:02:01";
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] au开发者_Go百科torelease];
[dateFormatter setDateFormat:@"EEE, dd MMM yyyy hh:mm:ss"];
NSDate *lDateFormatted = [dateFormatter dateFromString: lDateString ];

Any ideas appreciated, Thanks


You probably want to be using HH for hours, that's 0-23 / military style Also, you may need to put single ticks around your comma like ','

Reference: the UTS doc and date formatting guide.


The DateFormatter format is correct, but you've probably set the wrong locale. Try this one (working sample code of one of my projects):

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale *enUS = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[formatter setLocale:enUS];
[enUS release];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss"];
...
[formatter release]

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜