0
文字列から日付を取得しようとしていますが、常に1月の日付に日付を設定しています...なぜですか?DateFormatterで間違った日付が出力されない
コード:
NSMutableArray *dateArray = [NSMutableArray array];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"YYYYMMDD"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
for (id <NSFetchedResultsSectionInfo> sectionInfo in [fetchedResultsController sections]) {
NSLog(@"Adding date: %@", [sectionInfo name]);
NSDate *newDate = [dateFormatter dateFromString:[sectionInfo name]];
NSLog(@"Adding date 2: %@", newDate);
[dateArray addObject:newDate];
}
LOGS:
2012-03-01 15:14:48.124 MyApp[21793:fb03] Adding date: 20120827
2012-03-01 15:14:48.124 MyApp[21793:fb03] Adding date 2: 2012-01-27 00:00:00 +0000
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date: 20120830
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date 2: 2012-01-30 00:00:00 +0000
2012-03-01 15:14:48.125 MyApp[21793:fb03] Adding date: 20120831
2012-03-01 15:14:48.126 MyApp[21793:fb03] Adding date 2: 2012-01-31 00:00:00 +0000
2012-03-01 15:14:48.126 MyApp[21793:fb03] Adding date: 20120906
2012-03-01 15:14:48.127 MyApp[21793:fb03] Adding date 2: 2012-01-06 00:00:00 +0000
2012-03-01 15:14:48.127 MyApp[21793:fb03] Adding date: 20120907
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date 2: 2012-01-07 00:00:00 +0000
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date: 20120910
2012-03-01 15:14:48.128 MyApp[21793:fb03] Adding date 2: 2012-01-10 00:00:00 +0000
2012-03-01 15:14:48.129 MyApp[21793:fb03] Adding date: 20120913
2012-03-01 15:14:48.129 MyApp[21793:fb03] Adding date 2: 2012-01-13 00:00:00 +0000
それを修正しました。ありがとう。奇妙なのは 'yyyy'と' dd'がその月に影響していたのは、それが正しい文字だけだったからです。笑。 – Bot