'Get seconds since 1970 at start of day regardless of timezone
Say I've got User A and User B:
User A lives in GMT+9:30
User B lives in GMT+2:00
I've trying to construct a date at 12:00AM GMT for both users, so at any point in the day, both users get can the same timeIntervalSince1970.
In both timezones, constructing a date like
let date = Date()
date.timeIntervalSince1970 // 1650810600
Produces the same time, but if we try and shift the date to the start of the day,
let date = Calendar.current.startOfDay(for: Date())
date.timeIntervalSince1970 // GMT+9:30 1650810600.0
// GMT+2:00 1650837600.0
As you can see they're different.
So how can I create a Date object for 12:00 AM in GMT+0 in two timezones and produce the same date?
Solution 1:[1]
To create 12:00AM GMT for the current point in time you have to set the time zone of the calendar to GMT+0
var calendar = Calendar.current
calendar.timeZone = TimeZone(secondsFromGMT: 0)!
let date = calendar.startOfDay(for: Date())
date.timeIntervalSince1970
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | vadian |
