Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.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 dateFromComponents返回错误的日期_Ios_Objective C_Date_Nsdate_Nsdatecomponents - Fatal编程技术网

Ios dateFromComponents返回错误的日期

Ios dateFromComponents返回错误的日期,ios,objective-c,date,nsdate,nsdatecomponents,Ios,Objective C,Date,Nsdate,Nsdatecomponents,我正在使用此代码设置自定义日期 NSDecimalNumber *salaryToReturn = [NSDecimalNumber zero]; NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar NSDateComponents *components = [gregoria

我正在使用此代码设置自定义日期

NSDecimalNumber *salaryToReturn = [NSDecimalNumber zero];

NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSCalendarIdentifierGregorian]; // Setup an NSCalendar
NSDateComponents *components = [gregorianCalendar components: NSCalendarUnitDay | NSCalendarUnitMonth |  NSCalendarUnitYear |NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour | NSCalendarUnitTimeZone fromDate: [NSDate date]]; // Setup NSDateComponents
[components setTimeZone:[NSTimeZone defaultTimeZone]];
[components setMonth:monthInt]; // 6
[components setYear:yearInt]; // 2015

// Setup the new date
NSDate *dateToCompare = [gregorianCalendar dateFromComponents: components];
但是
dateToCompare
显示为“2015-07-01 11:31:01+0000”

与7月而不是6月一样,为什么会这样?但是,如果我将其设置为5而不是6,它会正确显示

谢谢大家!

编辑


我想我知道为什么,因为我没有设定日期,而且6月31日不存在。问题是我将当月还原为一个月,但这不起作用,因为6月只有30天,今天是31日。问题是我将当月还原为一个月,但这不起作用,因为六月只有30天,今天是31号,你忘了定日子了

[components setDay:1];

你忘了定日子了

[components setDay:1];