Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从当前日期算起一个月。_Ios_Date_Calendar_Nsdate_Nscalendar - Fatal编程技术网

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