質問

I'm trying to create an array of 30 days that takes in consideration of daylight savings time, leap year, and etc. I currently have a generator that makes an array of days but It's not taking in consideration of special time changes and year, month changes. Here's my current code:

    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);

What's the best way to create a generator to loop through the calendar to retrieve the next 30 days starting from today's date and the array should include today's date and the next 29 days. My current code works like I said but it's not totally accurate. Thanks

役に立ちましたか?

解決

You've almost got it; just a couple modifications:

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);

This will create an array of NSDate objects. On my machine, this logs:

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"
)

Note how the timezone offset changes on March 13th from -0800 to -0700. That's daylight savings time. :)

他のヒント

some code for my sidenote above:

- (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;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top