Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS one month from current Date.

I'm trying to get one month from the current date.

Below is what I have and it is returning: 4027-09-24 16:59:00 +0000. currentDate is right. What's going on?

// Get todays date to set the monthly subscription expiration date
NSDate *currentDate = [NSDate date];
NSLog(@"Current Date = %@", currentDate);
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit fromDate:currentDate];
dateComponents.month = dateComponents.month + 1;
NSDate *currentDatePlus1Month = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
NSLog(@"Date = %@", currentDatePlus1Month);
like image 533
Jon Erickson Avatar asked Dec 16 '22 05:12

Jon Erickson


1 Answers

Try this instead:

// Get todays date to set the monthly subscription expiration date
NSDate *currentDate = [NSDate date];
NSLog(@"Current Date = %@", currentDate);

NSDateComponents *dateComponents = [NSDateComponents new];
dateComponents.month = 1;

NSDate *currentDatePlus1Month = [[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:currentDate options:0];
NSLog(@"Date = %@", currentDatePlus1Month);
like image 113
liamnichols Avatar answered Dec 30 '22 12:12

liamnichols