我如何使用NSDateFormatter有什么问题? [英] What's wrong with how I'm using NSDateFormatter?

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

问题描述

   NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
   dateFormatter.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease];
   [dateFormatter setDateFormat:@"EEE, d MMM yyyy HH:mm:ss zzz"];
   NSString *dateString = @"Tue, 08 Jun 2010 17:00:00 EDT";
   NSDate *eventDate = [dateFormatter dateFromString:dateString];

在这种情况下,eventDate对象为nil。有人能告诉我吗?此代码曾经有效。

In this case the eventDate object is nil. Can somebody clue me in? This code used to work.

更新:
无法谈论为什么由于NDA而无效。我只想说,当iOS 4出局时,我会将答案发给我自己的问题。

UPDATE: Can't talk about why this doesn't work due to NDA. Suffice it to say, when iOS 4 is out I will post the answer to my own question.

推荐答案

这个问题的答案如下:
我使用了错误的日期格式字符串:

The answer to this question is the following: I was using the wrong date format string:

@"EEE, d MMM yyyy HH:mm:ss zzz"

应该是:

@"EEE, dd MMM y HH:mm:ss zzz"

关于iOS 4和NDA的部分是我认为我必须使用 NSDateFormatter 方法 dateFormatFromTemplate:options:locale: 看起来像这样:

The part about iOS 4 and NDA was that I thought I had to use the NSDateFormatter method dateFormatFromTemplate:options:locale: which would have looked like this:

NSString *format = [NSDateFormatter dateFormatFromTemplate:@"EEE, d MMM yyyy HH:mm:ss zzz" options:0 locale:[NSLocale currentLocale]];

但是,只有在想要将日期显示给未知语言环境的用户时才应使用该方法。在我的情况下,我确切地知道日期格式是什么样的,我试图将日期字符串PARSE,以便我可以将它存储在CoreData中。因此,该方法无用。

However, that method should only be used when you want to DISPLAY the date to a user of unknown locale. In my case, I knew exactly what the date format was going to look like and I was trying to PARSE the date string so that I could store it in CoreData. Therefore, that method wasn't useful.

奖励书签:仔细阅读此表,您一定会弄清楚问题所在。 .. Unicode日期格式应遵循以下规范: http://unicode.org/reports /tr35/tr35-6.html#Date_Field_Symbol_Table

Bonus bookmark: Read this table very carefully and you will definitely figure out what the problem is... Unicode date formats should follow these specifications: http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table

TL; DR 格式字符串错误。噢哦!

TL;DR The format string was wrong. D'oh!

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

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