convert eastern time to gmt in swift

To convert Eastern Time to GMT in Swift, you can use the DateFormatter and Calendar classes. Here's an example:

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

let eastern = "2022-01-01 12:00:00"
formatter.timeZone = TimeZone(identifier: "America/New_York")
let easternDate = eastern)!

formatter.timeZone = TimeZone(identifier: "GMT")
let gmtDate = formatter.string(from: easternDate) 

print(gmtDate) // Output: 2022-01-01 17:00:00
373 chars
12 lines

In this example, we first create a DateFormatter object and set its format to match the Eastern Time format. We then create a String variable with the Eastern Time we want to convert.

Next, we set the formatter's timeZone property to the Eastern Time zone ("America/New_York") and convert the String to a Date object.

Finally, we change the formatter's timeZone property to GMT ("GMT") and use it to convert the Date object back to a String representation in GMT time.

Note that the output is 2022-01-01 17:00:00 because Eastern Time is 5 hours behind GMT.

gistlibby LogSnag