2012-10-20 71 views
0

我正在使用NSDateFormatter將日期NSString實例轉換爲NSDate對象。在某一時刻,我意識到有些事情是不正確的,當我做一次檢查時,看起來NSDateFormatter在從字符串轉換爲NSDate時丟失了整整一年。NSDateFormetter失去一年

的代碼看起來是這樣的:

NSString *dateString = [dictionary objectForKey:@"match_date_time"]; 
if (dateString) { 
    // dateString is "2012-10-30 04:30:00" 

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]]; 
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; 

    self.date = [formatter dateFromString:dateString]; 
    // self.date ends up 2011-10-30 04:30:00 +0000, yes 2011 not 2012. 
} 

有誰知道爲什麼NSDateFormatter失敗如此慘敗?

謝謝!

回答

1

只寫了下面的代碼

[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
+0

是的,確實如此。一年中我使用了錯誤的符號。 http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table – Cyupa

+0

如果我對你有幫助,那麼你可以向上。 – freelancer

+0

需要8分鐘才能接受正確的答案。謝謝。 – Cyupa