此代碼適用於iOS 5,但它在iOS 6中返回了錯誤的一年。任何想法爲什麼?dateFromString在iOS 6中無法正常工作
NSCalendar *gregorian=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSLocale *locale=[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setCalendar: gregorian];
[dateFormatter setLocale: locale];
[dateFormatter setDateFormat:@"yyyy"];
NSDate* date=[dateFormatter dateFromString:[NSString stringWithFormat:@"9000"]];
NSLog(@"Date: %@", [dateFormatter stringFromDate:date]);
[gregorian release];
[locale release];
[dateFormatter release];
以上產生iOS 5中的日期9000和iOS 6中的日期2000!
謝謝。
如果你記錄日期,你會看到什麼? – rmaddy
將9000更改爲2012可以在iOS 5和iOS 6上獲得一致的結果.stringFromDate和原始NSDate在iOS 5和iOS 6中都是一致的,比如2012年。可能與9000有些奇怪! – Bourne
它適用於3512,但不適用於3513即它可以進入1500年,我猜 –