Midnights between NSDates

I'm trying to find a way of calculating the number of midnights between dates. For example, if one NSDate is "2016.07.17 23:00" and another is ""2016.07.18 01:00", I want to get a result of 1. But when I use this code:


let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm"

let date1 = formatter.dateFromString("2016-07-17 23:00")
let date2 = formatter.dateFromString("2016-07-18 01:00")

let calendar = NSCalendar.currentCalendar()
let day1 = calendar.ordinalityOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.Era, forDate: date1!)
let day2 = calendar.ordinalityOfUnit(NSCalendarUnit.Day, inUnit: NSCalendarUnit.Era, forDate: date2!)


day1 and day2 have the same value. I'm in the Pacific time zone, so I suppose the problem is that date1 and date2 have no midnight between them in GMT; for when I set the time of date1 to 16:00, day2 - day1 = 1.


How can I get this to work properly whatever the app user's local time zone?

What about using the startOfDayForDate: method instead of manually creating the date by setting the hour to 12?

Midnights between NSDates
 
 
Q