【问题标题】:How to generate 30 days as an array using NSCalendar or NSDate?如何使用 NSCalendar 或 NSDate 生成 30 天作为数组?
【发布时间】:2011-06-28 18:14:30
【问题描述】:

我正在尝试创建一个考虑夏令时、闰年等的 30 天数组。我目前有一个生成天数的生成器,但它没有考虑特殊的时间变化和年、月的变化。这是我当前的代码:

    NSMutableArray* dates = [[NSMutableArray alloc] init];
    int numberOfDays=30;
    NSDate *startDate=[NSDate date];
    NSDate *tempDate=[startDate copy];
    for (int i=0;i<numberOfDays;i++) {
        NSLog(@"%@",tempDate.description);
        tempDate=[tempDate dateByAddingTimeInterval:(60*60*24)];
        [dates addObject:tempDate.description];
    }

    NSLog(@"%@",dates);

创建一个生成器以循环遍历日历以检索从今天开始的接下来 30 天的最佳方法是什么,并且该数组应包括今天的日期和接下来的 29 天。我当前的代码就像我说的那样工作,但并不完全准确。谢谢

【问题讨论】:

    标签: iphone objective-c macos nsdate nscalendar


    【解决方案1】:

    你几乎明白了;只是几个修改:

    int numberOfDays=30;
    
    NSDate *startDate=[NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *offset = [[NSDateComponents alloc] init];
    NSMutableArray* dates = [NSMutableArray arrayWithObject:startDate];
    
    for (int i = 1; i < numberOfDays; i++) {
      [offset setDay:i];
      NSDate *nextDay = [calendar dateByAddingComponents:offset toDate:startDate options:0];
      [dates addObject:nextDay];
    }
    [offset release];
    
    NSLog(@"%@",dates);
    

    这将创建一个NSDate 对象数组。在我的机器上,这个日志:

    EmptyFoundation[4302:903] (
        "2011-02-16 16:16:26 -0800",
        "2011-02-17 16:16:26 -0800",
        "2011-02-18 16:16:26 -0800",
        "2011-02-19 16:16:26 -0800",
        "2011-02-20 16:16:26 -0800",
        "2011-02-21 16:16:26 -0800",
        "2011-02-22 16:16:26 -0800",
        "2011-02-23 16:16:26 -0800",
        "2011-02-24 16:16:26 -0800",
        "2011-02-25 16:16:26 -0800",
        "2011-02-26 16:16:26 -0800",
        "2011-02-27 16:16:26 -0800",
        "2011-02-28 16:16:26 -0800",
        "2011-03-01 16:16:26 -0800",
        "2011-03-02 16:16:26 -0800",
        "2011-03-03 16:16:26 -0800",
        "2011-03-04 16:16:26 -0800",
        "2011-03-05 16:16:26 -0800",
        "2011-03-06 16:16:26 -0800",
        "2011-03-07 16:16:26 -0800",
        "2011-03-08 16:16:26 -0800",
        "2011-03-09 16:16:26 -0800",
        "2011-03-10 16:16:26 -0800",
        "2011-03-11 16:16:26 -0800",
        "2011-03-12 16:16:26 -0800",
        "2011-03-13 16:16:26 -0700",
        "2011-03-14 16:16:26 -0700",
        "2011-03-15 16:16:26 -0700",
        "2011-03-16 16:16:26 -0700",
        "2011-03-17 16:16:26 -0700"
    )
    

    请注意 3 月 13 日时区偏移如何从 -0800 更改为 -0700。那是夏令时。 :)

    【讨论】:

    • 只是一个旁注:我会在一个月内得到实际天数,而不是硬编码 30 :-)(我知道这是他要求的)。
    • 感谢 Dave 提供代码,感谢 Rasmus 提供旁注。我将如何将您的代码 rasmus 实现到生成器?
    【解决方案2】:

    我上面的旁注的一些代码:

    - (NSRange) daysInMonth:(NSDate*)date {
    
        NSCalendar* cal = [NSCalendar currentCalendar];
        NSDateComponents *comps = [cal components:(NSYearCalendarUnit|NSMonthCalendarUnit) 
                                         fromDate:(date != nil) ? date: self.currentMonth];
    
        NSRange range = [cal rangeOfUnit:NSDayCalendarUnit
                                  inUnit:NSMonthCalendarUnit
                                 forDate:[cal dateFromComponents:comps]];
    
        return range;
    }
    

    【讨论】:

    • NSRange daysInMonth = [self daysInMonth:[NSDate date]]; int numberOfDays = daysInMonth.length;
    • +1 谢谢,但是它说我的视图控制器可能不响应 -daysInMonth 并且长度不是结构或联合。
    • 您需要将方法 daysInMonth 添加到您的 .h 文件中。
    • 我解决了 :-) NSCalendar* cal = [NSCalendar currentCalendar]; NSDateComponents* comps = [[[NSDateComponents alloc] init] autorelease]; NSRange 范围 = [cal rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:[cal dateFromComponents:comps]]; int numberOfDays = range.length;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-19
    相关资源
    最近更新 更多