开发者

Why does NSDateFormatter return nil?

开发者 https://www.devze.com 2022-12-19 20:40 出处:网络
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\";

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

精彩评论

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