How can i get next date using NSDate? [duplicate]
In the following, yourDate represents your input NSDate; nextDate represents the next day.
// start by retrieving day, weekday, month and year components for yourDate NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; NSDateComponents *todayComponents = [gregorian components:(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit) fromDate:yourDate]; NSInteger theDay = [todayComponents day]; NSInteger theMonth = [todayComponents month]; NSInteger theYear = [todayComponents year]; // now build a NSDate object for yourDate using these components NSDateComponents *components = [[NSDateComponents alloc] init]; [components setDay:theDay]; [components setMonth:theMonth]; [components setYear:theYear]; NSDate *thisDate = [gregorian dateFromComponents:components]; [components release]; // now build a NSDate object for the next day NSDateComponents *offsetComponents = [[NSDateComponents alloc] init]; [offsetComponents setDay:1]; NSDate *nextDate = [gregorian dateByAddingComponents:offsetComponents toDate:thisDate options:0]; [offsetComponents release]; [gregorian release];
what about this methods ?
http://www.drobnik.com/touch/2009/05/adding-days-to-nsdate/
NSDate *now = [NSDate date];int daysToAdd = 50; // or 60 :-)// set up date componentsNSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];[components setDay:daysToAdd];// create a calendarNSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];NSDate *newDate2 = [gregorian dateByAddingComponents:components toDate:now options:0];NSLog(@"Clean: %@", newDate2);