我有一個功能,通過使用NSDateFormatter
轉換爲NSDate
。NSDateFormatter IOS錯誤
+(NSDate*)getDateFromCurrentTimestamp:(NSString*)currentTimestamp{
// currentTimestamp= @"28-Feb-2012 16:42:19 PM";
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDoesRelativeDateFormatting:NO];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[dateFormatter setFormatterBehavior:NSDateFormatterShortStyle];
//[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehaviorDefault];
[dateFormatter setDateFormat:@"dd-MMM-yyyy HH:mm:ss a"];
return [dateFormatter dateFromString:currentTimestamp];
}
IOS 5.x返回正確NSDate
。 當在4.1上運行時& 4.2也返回正確的NSDate
。 當在4.3.2上運行時& 4.3.3,它返回null
。
怎麼可能?這個的任何解決方法?
編輯
這是不造成的iOS漏洞。這與手機本身有關。有些手機在設備中設置了不同的locale
。所以我們不得不迫使它使用一個locale
。
是你的'region'和'locale'設置爲4.3.2和4.3.3的設備?看看[這個問題](http://stackoverflow.com/questions/1559182/how-do-you-interpret-dates-with-nsdateformatter/1559284#1559284)幫助 – tipycalFlow 2012-04-02 04:46:56
是的,你可能是正確的。也許這個問題與'locale'不是ios版本有關。我會嘗試一下。 – HelmiB 2012-04-02 06:52:21
它的工作:'[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@「en_GB」] autorelease]];'。謝謝。太糟糕了,不能接受你的回答。 – HelmiB 2012-04-02 07:24:56