日期格式化程序在运行相同 iOS 版本的不同设备上有不同的输出

发布于 2024-12-11 11:49:08 字数 800 浏览 1 评论 0原文

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,
当前语言:自动;目前objective-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
当前语言:自动;目前objective-c 警告:无法读取 /SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib 的符号
这东西在模拟器上运行得很好。

这要怎么处理呢?

更多信息:
还需要注意的是,相同的格式化字符串会返回 12 小时和 24 小时时钟值

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);

Output in: Device iPhone4

[240:707]10/25/2011,22,23,

Current language: auto; currently objective-c
warning: Unable to read symbols for /SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib

Output in: Device iPad2

[82:707]10/25/2011,12,28,PM

Current language: auto; currently objective-c
warning: Unable to read symbols for /SDK4.2/Platforms/iPhoneOS.platform/DeviceSupport/4.3.3 (8J2)/Symbols/Developer/usr/lib/libXcodeDebuggerSupport.dylib


This thing works fine on Simulator for all.

How to handle this?

More Info:

it is also to notice that the same formatting string resulting in 12 hr and 24 hr clock values in return

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请你别敷衍 2024-12-18 11:49:08

听起来您在 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

但我错了,设置语言环境确实没有帮助。

引用:

<代码>
在 iPhone OS 上,用户可以覆盖默认的 AM/PM 与 24 小时时间设置(通过“设置”>“常规”>“日期和时间”>“24 小时时间”),这会导致 NSDateFormatter 重写您设置的格式字符串,这可能会导致您的时间解析失败。

Sounds like you have a different locale set on your iPad and your iPhone, see here:

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

Either set the same locale on both devices (if it's just your problem) or make sure the NSDateFormatter uses the same (hard-coded) locale if you rely on the format being like a certain format.

Edit: Additional Info

It apparently IS a problem with the locale, more detailed here (with solutions):
http://developer.apple.com/library/ios/#qa/qa1480/_index.html

But I was wrong, setting the locale will not help it indeed.

Citation:


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.

星星的轨迹 2024-12-18 11:49:08

终于解决了。
这是由于手机设置为 24 小时制所致。

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

:)

finally resolved it.
it was due to the settings in phone as 24 Hr clock.

I had to update my code accordingly.

:)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文