date formatter运行相同iOS版本的不同设备上的不同输出

时间:2011-10-25 07:04:12

标签: iphone ipad ios4

NSDate *currentDate =  [NSDate date];
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"MM/dd/yyyy,h,mm,a"];
NSString *dateString = [format stringFromDate:currentDate];
[format release];

NSLog(dateString);

输出:设备iPhone4
[240:707] 10/25 / 2011,22,23,
当前语言:auto;目前客观的c 警告:无法读取/SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3(8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib的符号

输出:设备iPad2
[82:707] 10/25 / 2011,12,28,PM
当前语言:auto;目前客观的c 警告:无法读取/SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3(8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib的符号
这个东西在模拟器上适用于所有人。

如何处理?

更多信息:
它还要注意到相同的格式化字符串导致返回12小时和24小时时钟值

2 个答案:

答案 0 :(得分:2)

听起来您在iPad和iPhone上设置了不同的区域设置,请参阅此处:

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369-SW1

要么在两个设备上设置相同的区域设置(如果只是你的问题),要么确保NSDateFormatter使用相同的(硬编码的)区域设置,如果你依赖的格式就像某种格式一样。

修改:其他信息

这显然是区域设置的一个问题,这里更详细(有解决方案): http://developer.apple.com/library/ios/#qa/qa1480/_index.html

但我错了,设置语言环境确实无济于事。

引用:

On iPhone OS, the user can override the default AM/PM versus 24-hour time setting (via Settings > General > Date & Time > 24-Hour Time), which causes NSDateFormatter to rewrite the format string you set, which can cause your time parsing to fail.

答案 1 :(得分:0)

终于解决了它。 这是由于手机中的设置为24小时。

我必须相应地更新我的代码。

:)