2010-11-16 56 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