I found this explanation very useful in the context of debugging an off-by-one-day bug in my crossword app. It's a classic scenario where crosswords are referenced by date but without a specific time, except that the date is usually reckoned by the time zone of the host publication.
I thought I would share an example of how I'm addressing the issue using the defaultDate technique, but generalizing it to choose a "middle of the day" time that should work with whatever time zone (Quinn's example uses a fixed relative time interval that is specific to Sao Paolo). Given a date formatter that already has the desired time zone set on it:
var midDayComponents = DateComponents(hour: 12)
midDayComponents.calendar = Calendar(identifier: .gregorian)
midDayComponents.timeZone = dateFormatter.timeZone
dateFormatter.defaultDate = midDayComponents.date
It seems that all you need is an hour, a calendar, and a time zone to come up with a suitably mid-day date. I hope this helps somebody!
Topic:
App & System Services
SubTopic:
General
Tags: