Ios 从当前日期算起一个月。
我想从当前日期算起一个月 下面是我所拥有的,它正在返回:Ios 从当前日期算起一个月。,ios,date,calendar,nsdate,nscalendar,Ios,Date,Calendar,Nsdate,Nscalendar,我想从当前日期算起一个月 下面是我所拥有的,它正在返回:4027-09-2416:59:00+0000。当前日期是正确的。发生什么事了 // Get todays date to set the monthly subscription expiration date NSDate *currentDate = [NSDate date]; NSLog(@"Current Date = %@", currentDate); NSDateComponents *dateComponents = [
4027-09-2416:59:00+0000
。当前日期是正确的。发生什么事了
// 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);
请尝试以下方法:
// 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);
你的代码增加了2013年11个月。。。到目前为止。谢谢兄弟,就这样!
NSDate *currentDate = [NSDate date];
NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate];
// Get necessary date components
[components month]; //gives you month
[components day]; //gives you day
[components year]; //gives you year