2015-10-08 start of a day with NSDate

Last edit

Changed:

< NSCalendar.currentCalendar().startOfDayForDate(NSDate())

to

> Calendar.current.startOfDay(for: Date())

Changed:

< NSCalendar.currentCalendar().startOfDayForDate(NSDate()).dateByAddingTimeInterval(-1)

to

> let thisMorning = Calendar.current.startOfDay(for: Date())
> let yesterdayEvening = Calendar.current.date(byAdding: .second, value: -
1, to: thisMorning)

Changed:

< if NSCalendar.currentCalendar().isDateInToday(flight.departureDate) {

to

> if Calendar.current.isDateInToday(flight.departureDate) {


There's a nice new function since iOS 9, startOfDayForDate. Some examples:

To get the NSDate for this morning:

 Calendar.current.startOfDay(for: Date())

And to get the end of the previous day:

 let thisMorning = Calendar.current.startOfDay(for: Date())
 let yesterdayEvening = Calendar.current.date(byAdding: .second, value: -1, to: thisMorning)

And since iOS 8, there's isDateInToday. For example:

 if Calendar.current.isDateInToday(flight.departureDate) {
     ....
 }