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
失敗如此慘敗?
謝謝!
是的,確實如此。一年中我使用了錯誤的符號。 http://unicode.org/reports/tr35/tr35-6.html#Date_Field_Symbol_Table – Cyupa
如果我對你有幫助,那麼你可以向上。 – freelancer
需要8分鐘才能接受正確的答案。謝謝。 – Cyupa