Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i get next date using NSDate? [duplicate]

Tags:

Possible Duplicate:
iOS and finding Tomorrow

How can i get next date using NSDate. Please send me the solution.

like image 946
Jai Avatar asked Jul 04 '09 06:07

Jai


1 Answers

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]; 
like image 119
Massimo Cafaro Avatar answered Sep 21 '22 16:09

Massimo Cafaro