首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IOS: NSDateComponent的问题

IOS: NSDateComponent的问题
EN

Stack Overflow用户
提问于 2011-04-20 16:47:01
回答 1查看 620关注 0票数 1

我有这样的代码:

代码语言:javascript
复制
- (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 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];
    }
}

这段代码设置了31个带有月份日期的标签,一切正常,但我不明白为什么10月份会有两个连续的工作日;例如:在今年,这段代码在月底的某一天这样写:

....25 26 27 28 29 30 30

30和30是红色的,但它不应该是这样的,它应该是

....25 26 27 28 29 30 31

只有30个必须是红色的

为什么会发生这种情况?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-20 16:52:50

这是因为夏令时的原因。我们在该循环中为每一天增加了86400秒,但一天将有25个小时。

编辑:

最好的方法可能就是把date对象也放在循环中,根本不做花哨的计算。

代码语言:javascript
复制
- (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];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5727722

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档