本文介绍了NSDateFormatter在12小时模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码。
NSDateFormatter *df = ...;
[df setTimeZone:[NSTimeZone defaultTimeZone]];
[df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZ"];
NSDate * date = [df dateFromString:date_string]; //here is the problem
在24小时模式下,一切都可以。当设备上设置了12小时模式时,stringFromDate返回null。
date_string的格式也是一样的,日期格式也是一样的。为什么会这样?
In 24-hour mode everything is ok. When 12-hour mode is set on device, stringFromDate returns null.Format of date_string is the same all the time, date format too. Why does it happen?
推荐答案
尝试以这种方式设置语言环境:
Try to set the locale in this way :
NSLocale *twelveHourLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
df.locale = twelveHourLocale;
要强制更换为24小时,您可以使用:
To force instead to 24 hour, you can use :
NSLocale *twentyFour = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];
这篇关于NSDateFormatter在12小时模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!