0

我确实构建了一个自定义日历,当我通过一周更改天数时,我看到日期为 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

我确实打破了我的大脑:)

4

2 回答 2

1

Yes, it was related to DST (daylight saving time). I just had replaced using of NSDateFormat for incrementing of day to 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];
}

It was very cool that DST is near and I found that bug before today submitting of the App.

Thanks to everyone. I guess it will help somebody in future.

于 2013-10-22T14:36:00.627 回答
-4

我认为你应该为你的 NSDateFormatter 对象设置日历,试试这个:

[formatter setCalendar:calendar];

stringFromDate:您应该在调用方法之前设置日历。

于 2013-10-21T23:12:25.183 回答