2010-11-16 58 views
0

我有一個nsdateformatteriphone - NSDateFormatter的問題?

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; 

的價值:2010-01-01 05:00:00.000 它返回正確的值

但值:2010-01-01 05:22:00.000 返回null。

根據文檔,如果我是正確的分鐘我想使用「mm」 那麼,我的代碼有什麼問題,爲什麼它返回零?

回答

4
NSDateFormatter * f = [[NSDateFormatter alloc] init]; 
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; 
NSLog(@"%@", [f dateFromString:@"2010-01-01 05:00:00.000"]); 
NSLog(@"%@", [f dateFromString:@"2010-01-01 05:22:00.000"]); 
[f release]; 

日誌:

2010-11-16 16:44:11.867 EmptyFoundation[42477:a0f] 2010-01-01 05:00:00 -0700 
2010-11-16 16:44:11.868 EmptyFoundation[42477:a0f] 2010-01-01 05:22:00 -0700 

所以它是正確地分析事物。因此,你在其他地方做錯了什麼。

+0

對不起我的錯,問題是其他地方在我的代碼 – aryaxt 2010-11-17 21:59:53

0

它看起來很乾淨。我試了一下,並得到正確的結果:

NSDateFormatter *f = [[NSDateFormatter alloc] init]; 
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; 
NSDate *date = [f dateFromString:@"2010-01-02 03:44:55.666"]; 

NSLog(@"%@",date); 
NSDateFormatter *f2 = [[NSDateFormatter alloc] init]; 
[f2 setDateFormat:@"d. MMMM yyyy HH mm ss SSS"]; 
NSString *s = [f2 stringFromDate:date]; 

NSLog(@"Date is really %@",s); 

返回下面的輸出:

2010-11-16 18:48:35.221 Test3[6407:207] 2010-01-02 03:44:55 -0500 
2010-11-16 18:48:35.223 Test3[6407:207] Date is really 2. January 2010 03 44 55 666