How to create 30 days as an array using NSCalendar or NSDate?

I am trying to create an array of 30 days that takes into account daylight saving time, leap year, etc. Currently, I have a generator that makes an array of days, but it does not take into account special changes in time and year, month changes. Here is 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 is the best way to create a generator to cycle through the calendar to get the next 30 days, starting from today’s date, and the array should contain today's date and the next 29 days. My current code works, as I said, but it is not entirely accurate. Thanks

+3
source share
2 answers

You almost got it; just a couple of 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);

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

, 13 -0800 -0700. .:)

+9

:

- (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;
}
+1

Source: https://habr.com/ru/post/1792677/


All Articles