我的問題是由NSDateComponents解決,但我想知道爲什麼這種方法可行,從而dateBySettingUnit方法返回不明顯結果
NSDate *today = [NSDate date];
NSLog(@"today is: %@", today);
NSDate *dayChanged = [calendar dateBySettingUnit:(NSCalendarUnitDay)
value:10
ofDate:today
options:0];
NSLog(@"dayChanged is: %@", dayChanged);
NSDate *monthChanged = [calendar dateBySettingUnit:(NSCalendarUnitMonth)
value:5
ofDate:today
options:0];
NSLog(@"monthChanged is: %@", monthChanged);
結果是:
today is: 2016-09-30 06:40:36 +0000
dayChanged is: 2016-10-10 00:00:00 +0000
monthChanged is: 2017-05-01 00:00:00 +0000
爲什麼dateBySettingUnit
增量下一個較大單元?
我明白 - 只有「未來日期」。謝謝你的答案 –