Question

I am wanting to list (NSLog) all the dates of the Georgian calendar year (2013). I have managed to get the current date using the following:

    NSCalendar*       calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents* components = [calendar components:NSDayCalendarUnit
                                               fromDate:currDate];
    NSInteger  day = [components day];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterLongStyle];
    NSString *dateString = [dateFormatter stringFromDate:currDate];
    NSLog(@"%@", dateString);

How can I print out all the dates in 2013?

Was it helpful?

Solution 2

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:@"2013-01-01"];


NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *components = [calendar components:kCFCalendarUnitYear fromDate:date];

while ([components year] < 2014) {
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateStyle:NSDateFormatterLongStyle];
    NSString *dateString = [dateFormatter1 stringFromDate:date];
    NSLog(@"%@", dateString);
    date = [NSDate dateWithTimeInterval:60*60*24 sinceDate:date];
    components = [calendar components:kCFCalendarUnitYear fromDate:date];
}

OTHER TIPS

NSDateFormatter* dateFormatter = [NSDateFormatter new] ;
dateFormatter.dateStyle = NSDateFormatterLongStyle ;

NSDate* today = [NSDate date] ;
NSCalendar* calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] ;
NSDateComponents* thisYearComponents = [calendar components:NSYearCalendarUnit fromDate:today] ;
NSDate* firstDateInThisYear = [calendar dateFromComponents:thisYearComponents] ;

NSDateComponents* addDaysComponents = [NSDateComponents new] ;
addDaysComponents.day = 0 ;

while ( TRUE ) {
    NSDate* nextDateInThisYear = [calendar dateByAddingComponents:addDaysComponents toDate:firstDateInThisYear options:0] ;
    NSDateComponents* yearOfNextDateComponents = [calendar components:NSYearCalendarUnit fromDate:nextDateInThisYear] ;
    if ( yearOfNextDateComponents.year == thisYearComponents.year )
        NSLog(@"%@", [dateFormatter stringFromDate:nextDateInThisYear]) ;
    else
        break ;
    addDaysComponents.day += 1 ;
}

The WWDC 2011 session 117 - Performing Calendar Calculations is a great source of information. It covers why it's better practice to, in a loop, add n days to a fixed reference date, rather than repeatedly adding 1 day to the most recently used date.

It also suggests using noon (12pm) instead of midnight (12am) for NSDates in which you don't care about the time, because Daylight Saving Time causes midnight to not exist for certain dates in certain places. But I didn't bother to do that in my example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top