How do I add 1 day to an NSDate?

Andrew picture Andrew · Feb 21, 2011 · Viewed 210.1k times · Source

Basically, as the title says. I'm wondering how I could add 1 day to an NSDate.

So if it were:

21st February 2011

It would become:

22nd February 2011

Or if it were:

31st December 2011

It would become:

1st January 2012.

Answer

Zaky German picture Zaky German · Feb 21, 2011

Swift 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)")

Objective 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);

This should be self-explanatory.