I have just started using the Swift 3.0 Calendar method startOfDay. It works great for the most part, but sometimes it has been returning a time of 3am in the returned date. I am getting a productionDate from a web service, which I am converting from a Unix style string using the following method
static let dateFormatString = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
static func dateForDateString(dateString: String) -> Date?
{
let formatter = DateFormatter()
formatter.dateFormat = dateFormatString
return formatter.date(from: dateString)
}
Example date/time string
2017-03-01T09:38:05-05:00
I am then saving an integer value (timeIntervalSince1970) to Firebase, where I can see the incorrect date as 3am. Here's a snippet of code I am using to generate the Int value I am saving to Firebase
let startofDay = Calendar.current.startOfDay(for: productionDate)
let dateValue = Int(startofDay.timeIntervalSince1970)
Like I said, most of the time, the time ends up being midnight of the correct date, but when the app is fetching from the web service in the background, I occasionally see 3am pop into the Firebase database.
I know I haven't given a ton of information here. I am really hoping someone has had the same problem and figured out a way to fix it or someone can look at my code and tell me how I am being a bonehead.
Thanks!