IOS:NSDateComponent 的问题

发布于 2024-11-02 14:01:29 字数 1493 浏览 4 评论 0原文

我有这个代码:

- (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 个带有月份日期的标签,一切都很好,但我不明白为什么十月份有连续两个工作日;举个例子:在今年,这段代码在月末一天这样写:

....25 26 27 28 29 30 30

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

....25 26 27 28 29 30 31

并且只有 30 必须是红色

为什么会发生这种情况?

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]];
}

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];
    }
}

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2024-11-09 14:01:30

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

编辑:

最好的方法可能是只在循环中获取日期对象,而根本不进行花哨的计算。

- (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];
}

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];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文