[Objective-C]NSDateComponentsの加算減算

スポンサーリンク

1/31に1日を足したら2/1になるような日付の計算はどうするのかって話

例:今日の1日後のNSDateComponentsを作る

NSDate *baseDate = [NSDate date]; // 足される元のNSDate
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 西暦のNSCalendar
NSDateComponents *offsetComps = [[NSDateComponents alloc] init];
offsetComps.day = 1;
NSDate *newDate = [calendar dateByAddingComponents:offsetComps toDate:baseDate options:0];

こんな感じ。引きたかったら-1とか入れる。

タイトルとURLをコピーしました