2013-03-29 49 views
0

目前,我有一個NSString的價值包含以下日期爲當前日期轉換的NSString的NSDate正在產生錯誤值

(lldb) po self.currentDate 
(NSString *) $5 = 0x002ca8d0 2013-03-29 

但是,當我這的NSString轉換成NSDate的,它顯示錯誤的值

NSDate *myCurrentDate = [dateFormatterForCurrentDate dateFromString:self.currentDate]; 

(lldb) po myCurrentDate 
(NSDate *) $4 = 0x002cf030 2013-03-28 18:30:00 +0000 
+1

**這是正確的!**這在時區顯示相對於GMT。 –

+0

Anoop是正確的。看到我的[對早期問題的答案](http://stackoverflow.com/questions/11273001/nsdateformatter-datefromstring-returns-incorrect-date/11273812#11273812)。 –

回答

2

設置此日期格式:

[dateFormatterForCurrentDate setDateFormat:@"yyyy-MM-dd"]; 

,並檢查Date Formatters in developer.apple.com

可能對您有用。

3

嘗試用這個代碼,考慮到本地時區....

NSDateFormatter *dateFormatterForCurrentDate = [[NSDateFormatter alloc] init]; 
[dateFormatterForCurrentDate setDateFormat:@"yyyy-MM-dd"]; 
[dateFormatterForCurrentDate setTimeZone:[NSTimeZone localTimeZone]]; 
NSDate *myCurrentDate = [dateFormatterForCurrentDate dateFromString:self.currentDate]; 

希望這是有益...