Constructing a date for getRequestedUpdateDateWithHandler:

42 Views Asked by At

I need to update my watchOS complication at midnight every day.

startOfDay is the beginning of the day (i.e., 12 AM today).

Should I add a day to the start of today like this?

func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
    // Call the handler with the date when you would next like to be given the opportunity to update your complication content
    let startOfDay = NSDate().startOfDay
    let components = NSDateComponents()
    components.day = 1
    let startOfNextDay = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: startOfDay, options: NSCalendarOptions())
    handler(startOfNextDay)
}

Or should I not add a day to the code, and just do something like this:

func getNextRequestedUpdateDateWithHandler(handler: (NSDate?) -> Void) {
    // Call the handler with the date when you would next like to be given the opportunity to update your complication content
    let startOfDay = NSDate().startOfDay
    handler(startOfDay)
}
1

There are 1 best solutions below

0
On BEST ANSWER

You'd want to advance the date one day, since you want your next requested update to occur at tomorrow's midnight. The first method would do what you want, but you can simplify it as follows:

let calendar = NSCalendar.currentCalendar()
let startOfDay = calendar.startOfDayForDate(NSDate())
let startOfNextDay = calendar.dateByAddingUnit(.Day, value: 1, toDate: startOfDay, options: NSCalendarOptions())!

The second code would return today's 12 AM, which would already be in the past.