NSDateに1日追加するにはどうすればいいですか? 質問する

NSDateに1日追加するにはどうすればいいですか? 質問する

基本的にはタイトルの通りです。 に 1 日を追加するにはどうしたらよいかと思っていますNSDate

つまり、次のようになります。

21st February 2011

次のようになります:

22nd February 2011

あるいは、次のようになると:

31st December 2011

次のようになります:

1st January 2012.

ベストアンサー1

スウィフト5.0:

var dayComponent    = DateComponents()
dayComponent.day    = 1 // For removing one day (yesterday): -1
let theCalendar     = Calendar.current
let nextDate        = theCalendar.date(byAdding: dayComponent, to: Date())
print("nextDate : \(nextDate)")

目標C:

NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;

NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];

NSLog(@"nextDate: %@ ...", nextDate);

これは説明するまでもないはずです。

おすすめ記事