我确实构建了一个自定义日历,当我通过一周更改日期的日期时,我看到日期为26时的奇怪情况,但NSDateFormatter和NSDateComponents返回27,同样是27 = 27,你可以看到来自的日志。在另一种情况下,31是30,并且1是31
有人有类似的东西吗?
for (NSDictionary *dict in weekViews) {
UILabel *dayName = dict[@"dayName"];
UILabel *dayNumber = dict[@"dayNumber"];
components = [calendar components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:tempDate];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd";
//TODO:Remove NSLog
NSLog(@"%@ = %@", [formatter stringFromDate:tempDate], tempDate);
dayName.text = [NSString stringWithFormat:@"%@", weekDaySymbols[index++]];
dayNumber.text = [NSString stringWithFormat:@"%d", components.day];
tempDate = [tempDate dateByAddingTimeInterval:86400];
}
2013-10-22 01:06:25.812护理[539:70b] 27 = 2013-10-26 21:00:00 +0000
2013-10-22 01:06:25.813护理[539:70b] 27 = 2013-10-27 21:00:00 +0000
2013-10-22 01:06:25.813护理[539:70b] 28 = 2013-10-28 21:00:00 +0000
2013-10-22 01:06:25.814护理[539:70b] 29 = 2013-10-29 21:00:00 +0000
2013-10-22 01:06:25.814护理[539:70b] 30 = 2013-10-30 21:00:00 +0000
2013-10-22 01:06:25.815护理[539:70b] 31 = 2013-10-31 21:00:00 +0000
2013-10-22 01:06:25.816护理[539:70b] 01 = 2013-11-01 21:00:00 +0000
该代码的另一个例子:
2013-10-22 01:14:12.456护理[539:70b] 22 = 2014-03-22 21:00:00 +0000
2013-10-22 01:14:12.456护理[539:70b] 23 = 2014-03-23 21:00:00 +0000
2013-10-22 01:14:12.457护理[539:70b] 24 = 2014-03-24 21:00:00 +0000
2013-10-22 01:14:12.457护理[539:70b] 25 = 2014-03-25 21:00:00 +0000
2013-10-22 01:14:12.458护理[539:70b] 26 = 2014-03-26 21:00:00 +0000
2013-10-22 01:14:12.458护理[539:70b] 27 = 2014-03-27 21:00:00 +0000
2013-10-22 01:14:12.459护理[539:70b] 28 = 2014-03-28 21:00:00 +0000
2013-10-22 01:12:34.942护理[539:70b] 29 = 2014-03-29 21:00:00 +0000
2013-10-22 01:12:34.943护理[539:70b] 31 = 2014-03-30 21:00:00 +0000
2013-10-22 01:12:34.943护理[539:70b] 01 = 2014-03-31 21:00:00 +0000
2013-10-22 01:12:34.944护理[539:70b] 02 = 2014-04-01 21:00:00 +0000
2013-10-22 01:12:34.944护理[539:70b] 03 = 2014-04-02 21:00:00 +0000
2013-10-22 01:12:34.945护理[539:70b] 04 = 2014-04-03 21:00:00 +0000
2013-10-22 01:12:34.945护理[539:70b] 05 = 2014-04-04 21:00:00 +0000
我确实打破了我的大脑:)
答案 0 :(得分:1)
是的,它与夏令时(夏令时)有关。我刚刚用NSDateFormat替换了将日期递增到NSDateComponent。
NSDate *tempDate = currentDate;
for (NSDictionary *dict in weekViews) {
UILabel *dayName = dict[@"dayName"];
UILabel *dayNumber = dict[@"dayNumber"];
components = [calendar components:NSCalendarUnitMonth | NSCalendarUnitDay fromDate:tempDate];
dayName.text = [NSString stringWithFormat:@"%@", weekDaySymbols[index++]];
dayNumber.text = [NSString stringWithFormat:@"%d", components.day];
components.day++;
tempDate = [calendar dateFromComponents:components];
}
DST临近很酷,我在今天提交应用程序之前发现了这个错误。
感谢大家。我想这将有助于将来的某些人。
答案 1 :(得分:-4)
我认为你应该为你的NSDateFormatter对象设置日历,试试这个:
[formatter setCalendar:calendar];
您应该在调用stringFromDate:
方法之前设置日历。