NSDate from NSString when time is in 24 hr format

I have a string of the format Jan 25, 2011 3:17 AM. I need to convert it to NSDate. I used NSDateFormatter with format @"MMM d, yyyy h:mm a". It Works well if iphone time is in 12 hr format, but returns nil if time is in 24 hr format. Can any one help me with this????


Capital H is used for 24 hour format. Don't 24 hour times usually exclude the AM/PM part? If so, your format string should be: @"MMM d yyyy H:mm" .

Here's a reference for Unicode date format strings.


It's a bug in NSDateFormatter. You can work around it by manually setting a locale on the date formatter:

[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"] autorelease]];
链接地址: http://www.djcxy.com/p/35162.html

上一篇: NSDate格式问题

下一篇: 当时间为24小时格式时,NSString的NSDate