2009-10-13 44 views
1

在Apple Docs中,他們說NSDateFormatter使用Unicode作爲規範。我讀過的規格,但我有麻煩解釋此日期:如何用NSDateFormatter解釋日期?

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) 

回答

2

對不起鄉親,琢磨出來了。 NSDateFormatter默認使用當前系統區域設置。

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

我:在上面的代碼示例中,如果您將系統區域設置爲除美國要解決它的東西,只是之前的區域設置爲你的日期字符串中使用的,這樣它只有失敗我只是遇到了同樣的問題。這與當前的日期和時間設置(12/24小時)有關。我試着用'yyyy-MM-dd HH:mm:ss'解析日期'2004-11-29 16:01:26'。只有當我強制locale時纔有效:'[dateFormatter setLocale:[[[[NSLocale alloc] initWithLocaleIdentifier:@「en_GB」] autorelease]];'混亂但是可行! – 2009-12-30 02:35:51