Objective c 使用12小时时间格式时,NSDateFormatter stringFromDate返回零

Objective c 使用12小时时间格式时,NSDateFormatter stringFromDate返回零,objective-c,ios,ios5,nsdateformatter,Objective C,Ios,Ios5,Nsdateformatter,以下代码适用于24小时时间格式 +(NSString*)格式日期:(NSDate*)日期使用LongStyle:(BOOL)使用LongStyle显示日期:(BOOL)显示日期显示时间:(BOOL)显示时间 { NSDateFormatter*dateFormatter=[NSDateFormatter new]; dateFormatter.dateStyle=(useLongStyle)?NSDateFormatterLongStyle:NSDateFormatterShortStyle;

以下代码适用于24小时时间格式

+(NSString*)格式日期:(NSDate*)日期使用LongStyle:(BOOL)使用LongStyle显示日期:(BOOL)显示日期显示时间:(BOOL)显示时间
{
NSDateFormatter*dateFormatter=[NSDateFormatter new];
dateFormatter.dateStyle=(useLongStyle)?NSDateFormatterLongStyle:NSDateFormatterShortStyle;
dateFormatter.dateStyle=(showDate)?dateFormatter.dateStyle:NSDateFormatterNoStyle;
dateFormatter.timeStyle=(showTime)?NSDateFormatterShortStyle:NSDateFormatterNoStyle;
返回[dateFormatter stringFromDate:date];
}
但当手机设置中有12小时格式时,它会返回
nil
。直到我明确地将区域设置为例如澳大利亚


日期中的时间为24小时格式。当前区域设置是
ru_-ru
(但在
en_-en
中是相同的)。

我遇到了类似的问题,并找到了两种解决方案。一个是手动设置区域设置(我选择en_GB作为24小时格式),另一个是设置时区。两人都为我单独工作。如果您不打算处理需要根据时区更改的日期,我建议您选择设置区域设置,因为在处理时区时,源时区和目标时区将发挥作用,除非绝对必要,否则您不需要处理所有这些。因此,请尝试设置区域设置,否则请选择时区。不知何故,它帮助覆盖了系统设置

[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
你可以用这个

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
    [formatter setLocale:locale];

你在用网络约会吗?有一个已知的错误:@wquist你是说那种格式:
2010-11-29 09:00:00 PM+0000
?不,那种格式应该可以。。。您是否尝试过将日期格式设置为:
[dateFormatter setDateFormat:@“MM/dd/yyyy HH:MM:ss ZZZ”]不确定您所说的“日期中的时间是24小时格式的”。NSDate只是时间戳的包装器;它没有固有的格式。FWIW,我在en_US(12小时设置)中尝试了上述代码,效果很好。您确定传入的
日期
不是零吗?