你如何用NSDateFormatter解释日期? [英] How do you interpret dates with NSDateFormatter?

查看:90
本文介绍了你如何用NSDateFormatter解释日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Apple Docs中,他们说NSDateFormatter使用Unicode作为规范。我已经阅读了规范,但是我在解释这个日期时遇到了麻烦:

In the Apple Docs, they say that NSDateFormatter uses the Unicode for spec. I've read the spec, but I'm having troubles interpreting this date:

NSDateFormatter *frm = [[NSDateFormatter alloc] init];
[frm setDateFormat:@"EEE MMM dd HH:mm:ss ZZZ yyyy"];
NSLog(@"Tue Oct 13 09:24:15 +0000 2009 becomes %@", [frm dateFromString:@"Tue Oct 13 09:24:15 +0000 2009"]);

此代码打印出来:

2009-10-13 11:37:40.334 test2[1704:20b] Tue Oct 13 09:24:15 +0000 2009 becomes (null)


推荐答案

对不起,伙计们已经知道了。 NSDateFormatter默认使用当前系统区域设置。在上面的代码示例中,只有在将系统区域设置设置为US以外的其他内容时才会失败。要修复它,就在将区域设置设置为日期字符串使用的区域设置之前,如下所示:

Sorry folks, figured it out already. NSDateFormatter defaults to using the current system locale. In the code example above, it only fails if you set your system locale to something other than U.S. To fix it, just before to set the locale to the one used by your date string, like this:

[frm setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];

这篇关于你如何用NSDateFormatter解释日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆