开发者

IOS: problem with NSDateComponent

I have this code:

- (void) setDataLabel{

for (int k = 0; k<31; k++){

    [[lineSunday objectAtIndex:k] setAlpha:0.00];
    [[arrayDay objectAtIndex:k] setTextColor:[UIColor whiteColor]];
}

NSDateCompon开发者_Go百科ents *components = [[[NSDateComponents alloc] init] autorelease];
[components setYear:2011];
[components setDay:1];
[components setMonth:10];
//NSLog(@"mese:%d", month);
NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *firstDate = [gregorianCalendar dateFromComponents:components];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd"];

for (int i = 0; i < 31; i++) {
    NSTimeInterval seconds = 24*60*60 * i;
    NSDate *date = [NSDate dateWithTimeInterval:seconds sinceDate:firstDate];
    NSDateComponents *weekdayComponents = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:date];
    int weekday = [weekdayComponents weekday];
    NSString *strDate = [dateFormatter stringFromDate: date];
    [[arrayDay objectAtIndex:i] setText:strDate];
    if (weekday == 1) {
        [[arrayDay objectAtIndex:i] setTextColor:[UIColor redColor]];
        [[lineSunday objectAtIndex:i] setAlpha:1.00];
    }
}

This code set 31 labels with days of the month and it's all ok, but I don't understand why for October month there is ever two weekday consecutive; an example: in this year this code write at the end of the month the day in this way:

....25 26 27 28 29 30 30

and 30 and 30 are red colour, but it shouldn't be so, it should be

....25 26 27 28 29 30 31

and only 30 must be redcolour

Why it happen?


It's because of daylight saving time. We are adding 86400 seconds for each day in that loop, but one day will have 25 hours.

Edit:

Best approach is probably to just get the date object in the loop as well and not doing fancy calculations at all.

- (void) setDataLabel{

    for (int k = 0; k<31; k++){
        [[lineSunday objectAtIndex:k] setAlpha:0.00];
        [[arrayDay objectAtIndex:k] setTextColor:[UIColor whiteColor]];
    }

    NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
    [components setYear:2011];
    [components setMonth:10];
    NSCalendar *gregorianCalendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd"];

    for (int i = 0; i < 31; i++) {
        [components setDay:i+1];
        NSDate *date = [gregorianCalendar dateFromComponents:components];
        NSDateComponents *weekdayComponents = [gregorianCalendar components:NSWeekdayCalendarUnit fromDate:date];
        int weekday = [weekdayComponents weekday];
        NSString *strDate = [dateFormatter stringFromDate: date];
        [[arrayDay objectAtIndex:i] setText:strDate];
        if (weekday == 1) {
            [[arrayDay objectAtIndex:i] setTextColor:[UIColor redColor]];
            [[lineSunday objectAtIndex:i] setAlpha:1.00];
        }
    }
    [dateFormatter release];
    [gregorianCalendar release];
    [components release];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜