2
我想弄清楚如何解析AD和BC日期與NSDateFormatter和爲什麼0000年解析失敗。NSDateFormatter BC/AD日期問題
NSString *test1 = @"0000-01-01";
NSString *test2 = @"0001-01-01";
NSString *test3 = @"0002-01-01 AD";
NSString *test4 = @"0002-01-01 BC";
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat: @"yyyy-MM-dd"];
NSLog(@"test 1 - %@", [formatter1 dateFromString: test1]);
NSLog(@"test 2 - %@", [formatter1 dateFromString: test2]);
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
[formatter2 setDateFormat: @"yyyy-MM-dd G"];
NSLog(@"test 3 - %@", [formatter2 dateFromString: test3]);
NSLog(@"test 4 - %@", [formatter2 dateFromString: test4]);
而這正是它返回:
2012-11-26 11:24:27.536 Test[1968:c07] test 1 - (null)
2012-11-26 11:24:28.615 Test[1968:c07] test 2 - 0001-12-31 22:21:00 +0000
2012-11-26 11:24:37.604 Test[1968:c07] test 3 - 0001-12-31 22:21:00 +0000
2012-11-26 11:24:45.738 Test[1968:c07] test 4 - 0003-12-31 22:21:00 +0000
正如你可以看到它對於其他變種0000的日期和錯誤的日期返回null。
**沒有第0年** – 2012-11-26 09:30:54
但是BC/AD年呢?他們也回來錯誤的日期! –
,因爲設置爲日期格式化程序的格式與日期字符串的格式不匹配。 – 2012-11-26 10:07:48