开发者

NSDate from NSString when time is in 24 hr format

开发者 https://www.devze.com 2023-02-07 12:13 出处:网络
I have a string o开发者_如何学Gof the format Jan 25, 2011 3:17 AM. I need to convert it to NSDate.

I have a string o开发者_如何学Gof 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]];
0

精彩评论

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