Author Topic: How can I add a number of days to an NSDate?  (Read 2337 times)

kjmcneish

  • Administrator
  • *****
  • Posts: 719
    • View Profile
How can I add a number of days to an NSDate?
« on: July 11, 2014, 05:08:25 PM »
The following code demonstrates how to add one day to an NSDate value.

Swift:
Code: [Select]
let dayComponent = NSDateComponents()
dayComponent.day = 1

let theCalendar = NSCalendar.currentCalendar()
let nextDate = theCalendar.dateByAddingComponents(dayComponent, toDate: NSDate(), options: NSCalendarOptions.allZeros)

Code: [Select]
NSDateComponents *dayComponent = [[NSDateComponents alloc] init];
dayComponent.day = 1;
   
NSCalendar *theCalendar = [NSCalendar currentCalendar];
NSDate *nextDate = [theCalendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0];

All the best!
Kevin McNeish
Author of "Learn to Code in Swift": https://itunes.apple.com/us/book/learn-to-code-in-swift/id942956811?mt=11
« Last Edit: July 21, 2015, 02:31:04 AM by kjmcneish »