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]
精彩评论