2010-09-15 73 views
1

[NSDate的日期]的iOS 4.0中使用返回日期的格式爲: 2010-09-15 9時28分26秒0530的NSDate格式4.1

但現在在iOS 4.1的它返回的日期格式: 2010-09-15 09:28:26 GMT

在我的應用程序中導致了很多問題。

有誰知道如何解決這個問題?

由於提前, YPK

+0

顯示您用於「獲取日期」的示例代碼...和(更可能是問題)...顯示您用於「顯示日期」的代碼。 – Patricia

回答

3

感謝您的答覆。我搜索了這個問題,並發現NSDate在iOS 4.1中有一個bug。所以,我用下面的方法

- (NSString *)formattedStringUsingFormat:(NSString *)dateFormat 
{ 
    NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
    [formatter setDateFormat:dateFormat]; 
    [formatter setCalendar:cal]; 
    [formatter setLocale:[NSLocale currentLocale]]; 
    NSString *ret = [formatter stringFromDate:[NSDate date]]; 
    [formatter release]; 
    [cal release]; 
    return ret; 
} 

解決了這個問題,我通過了格式

[self formattedStringUsingFormat:@"yyyy-MM-dd HH:mm:ss ZZ"]; 
1

在iOS2.x和3.x的NSDate的描述/ datefromstring函數返回: 2010-09-24 17:30:00 - 0700

在iOS 4.1的它返回: 2010-09-25 00:30:00 GMT

你可以提交一個bug蘋果。