NSDateFormatter为@“dd-MM-yy”返回nil在iOS 3.0 [英] NSDateFormatter returns nil for @"dd-MM-yy" in iOS 3.0

查看:151
本文介绍了NSDateFormatter为@“dd-MM-yy”返回nil在iOS 3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这部分代码:

NSDate *date =nil;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"dd-MM-yy"];
date = [dateFormatter dateFromString:inString];
[dateFormatter release];

它的工作原理完全正常,如预期在iOS 4.0。但是3.0中的代码不一样。

It works perfectly fine, as expected in iOS 4.0. But the same code doesnt in 3.0.

我得到的字符串就像12-Nov-10,并且包含在inString指针中。

The string which I am getting, is like "12-Nov-10" and this is contained in inString pointer.

如果本机操作系统是3.0或3.1,日期格式化程序返回nil。由于某些原因,我需要坚持使用相同的日期格式。有没有人面临这个问题?有任何建议可解决此问题吗?

The date formatter returns nil if the native OS is 3.0 or 3.1. For some reasons I need to stick to the same date format. Has anyone else faced this problem? Any suggestions to resolve this issue?

感谢,

Raj

修改
在遵循Harkonian和Q& A讨论指出的建议后,输入正确的代码:

The proper code, after following suggestions pointed out by Harkonian and the Q&A discussions:

NSDate *date =nil;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateFormat:@"dd-MMM-yy"];

NSLocale *enUSPOSIXLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[dateFormatter setLocale:enUSPOSIXLocale];
[enUSPOSIXLocale release];

date = [dateFormatter dateFromString:inString];
[dateFormatter release];


推荐答案

如果您使用用户可见的日期,您应避免设置日期格式字符串,因为很难预测在所有可能的用户配置中如何表示格式字符串。相反,你应该尝试并限制自己设置日期和时间样式(通过 - [NSDateFormatter setDateStyle:]和 - [NSDateFormatter setTimeStyle:])。

If you're working with user-visible dates, you should avoid setting a date format string because it's very hard to predict how your format string will be expressed in all possible user configurations. Rather, you should try and limit yourself to setting date and time styles (via -[NSDateFormatter setDateStyle:] and -[NSDateFormatter setTimeStyle:]).

,如果您使用固定格式的日期,您应该首先将日期格式化程序的区域设置设置为适合您的固定格式的区域设置。在大多数情况下,要选择的最佳区域设置是en_US_POSIX,这是一个专门设计用于生成美国英语结果的区域设置,而不管用户和系统首选项。 en_US_POSIX在时间上也是不变的(如果美国,在未来的某个时候改变了格式化日期的方式,en_US将改变以反映新的行为,但是en_US_POSIX不会改变) en_US_POSIX在iPhone操作系统上的工作方式与在Mac OS X上的操作相同,就像在其他平台上一样。)

On the other hand, if you're working with fixed-format dates, you should first set the locale of the date formatter to something appropriate for your fixed format. In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences. "en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iPhone OS as it does on Mac OS X, and as it it does on other platforms).

一旦设置了en_US_POSIX作为日期格式化程序的区域设置,您可以为所有用户设置日期格式字符串和日期格式化程序的一致行为。

Once you've set "en_US_POSIX" as the locale of the date formatter, you can then set the date format string and the date formatter will behave consistently for all users.

可以找到上述信息在Apple的技术问答QA1480

The above info and more can be found in Apple's Technical Q&A QA1480

下面是我应用程序中的代码片段,它实现了上述建议:

Here's a snippet of code from my app which implements the above recommendation :

static NSDateFormatter* dateFormatter = nil;
if (!dateFormatter) {
   dateFormatter = [[NSDateFormatter alloc] init];

   NSLocale *enUSPOSIXLocale = [[[NSLocale alloc] 
      initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];
   assert(enUSPOSIXLocale != nil);
   [dateFormatter setLocale:enUSPOSIXLocale];
   [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
   dateFormatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss +0000";
}

这篇关于NSDateFormatter为@“dd-MM-yy”返回nil在iOS 3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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