NSDateFormatter未显示完整月份名称,仅显示单个数字

Chr*_*ris 27 nsdate nsdateformatter ios

不确定是什么问题,但不是得到一个月的名字"七月",我得到"07".

dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setLocale:[NSLocale systemLocale]];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];    
[dateFormat setDateFormat:@"MMMM dd, h:mm a"];
Run Code Online (Sandbox Code Playgroud)

我已经尝试了M,MM,MMM,MMMM,并且所有这些都给了我一个数字,而不是月份名称,尽管有不同数量的前导0.

Chr*_*ris 52

原来是第二行的问题,setLocale.我假设locale在手动设置时系统不会默认使用英文月份名称?我住在英语口语中locale,但也许没关系.

在任何情况下,不设置区域设置都会修复月份名称问题.

dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setTimeZone:[NSTimeZone localTimeZone]];    
[dateFormat setDateFormat:@"MMMM dd, h:mm a"];
Run Code Online (Sandbox Code Playgroud)


J2t*_*heC 13

问题是语言环境,而不是格式.看一下这个:

http://waracle.net/mobile/iphone-nsdateformatter-date-formatting-table/

基本上,"MMMM"将为您提供本月的全名.


Fab*_*ser 8

-systemLocale这不是你想要的.如果没有其他语言环境适合,它将返回一个回退语言环境.使用-currentLocale来获取用户当前的语言环境.

另外:
你应该使用NSDateFormatter+dateFormatFromTemplate:options:locale:方法来获得正确的日期格式.在某些语言中,这一天是在月份之前,等等,反之亦然.