To convert Eastern Time to GMT in Swift, you can use the DateFormatter
and Calendar
classes. Here's an example:
main.swift373 chars12 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