来自 NSString 的 NSDate
我已阅读 DateFormatting 指南,但仍然无法获得可用的格式化程序。
NSString *string = @"0901Z 12/17/09";
//This is a sample date. The Z stands for GMT timezone
//The 0901 is 09h 01m on a 24 hour clock not 12.
//As long as I can get the hours/min & date from the string I can deal with the time zone later
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hhmm'Z' MM/dd/yy"];
NSDate *date = [dateFormat dateFromString:string];
I've read the DateFormatting guide and I'm still not able to get a working formatter.
NSString *string = @"0901Z 12/17/09";
//This is a sample date. The Z stands for GMT timezone
//The 0901 is 09h 01m on a 24 hour clock not 12.
//As long as I can get the hours/min & date from the string I can deal with the time zone later
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"hhmm'Z' MM/dd/yy"];
NSDate *date = [dateFormat dateFromString:string];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当我尝试时这对我有用。将
NSLog(@"%@", date)
添加到代码末尾会给出以下输出:您遇到的问题是什么?
编辑:我发现,您对
09:01
没有问题,但对其他 24 小时时间有问题,例如14:25
,对吧?将格式化程序更改为:That works for me when I try it. Adding
NSLog(@"%@", date)
to the end of your code gives me this output:What is the problem you're seeing?
Edit: I figured it out, you're not having a problem with
09:01
, but with other 24-hour times, like14:25
, right? Change your formatter to:从我在这里回答的类似问题复制: NSDateFormatter 在 iOS 3.0 中对 @"dd-MM-yy" 返回 nil
如果您正在使用用户可见的日期,则应避免设置日期格式字符串。以这种方式格式化日期是不可本地化的,并且无法预测格式字符串在所有可能的用户配置中的表达方式。相反,您应该尝试限制自己设置日期和时间样式(通过 -[NSDateFormatter setDateStyle:] 和 -[NSDateFormatter setTimeStyle:])。
另一方面,如果您使用固定格式日期,则应首先将日期格式化程序的区域设置设置为适合您的固定格式的区域设置。在大多数情况下,选择的最佳区域设置是“en_US_POSIX”,该区域设置专门设计用于生成美国英语结果,无论用户和系统首选项如何。 “en_US_POSIX”在时间上也是不变的(如果美国在未来的某个时刻改变了日期格式的方式,“en_US”将改变以反映新的行为,但“en_US_POSIX”不会),并且在机器之间( “en_US_POSIX”在 iPhone OS 上的工作方式与在 Mac OS X 上以及在其他平台上的工作方式相同)。
将“en_US_POSIX”设置为日期格式化程序的区域设置后,您就可以设置日期格式字符串,并且日期格式化程序将对所有用户保持一致的行为。
上述信息及更多信息可以在 Apple 的技术问答 QA1480 中找到
这是我的应用程序中实现上述建议的代码片段:
Copied from a similar question I answered here: NSDateFormatter returns nil for @"dd-MM-yy" in iOS 3.0
If you're working with user-visible dates, you should avoid setting a date format string. Formatting dates this way is not localizable and makes it impossible 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:]).
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).
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.
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 :