convert hawaii time to gmt in swift

Here's an example code on how to convert Hawaii time to GMT in Swift:

main.swift
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
formatter.timeZone = TimeZone(identifier: "Pacific/Honolulu") // set time zone to Hawaii
let hawaiiTime = formatter.date(from: "2021-10-03 15:30:00") // sample Hawaii time

formatter.timeZone = TimeZone(identifier: "GMT") // set time zone to GMT
let gmtTime = formatter.string(from: hawaiiTime!) // convert Hawaii time to GMT

print(gmtTime) // output: "2021-10-04 01:30:00"
452 chars
10 lines

In the example above, we first create a DateFormatter instance and set the dateFormat property to match the format of the input date string. We also set the timeZone property of the formatter to "Pacific/Honolulu", which is the time zone for Hawaii.

Next, we use the formatter object to convert the input date string ("2021-10-03 15:30:00") to a Date object. Since the input date string is in the Hawaii time zone, the resulting Date object will also be in the Hawaii time zone.

We then change the time zone property of the formatter object to "GMT", which is the time zone we want to convert to. We use the formatter object again to convert the Hawaii time Date object to a string in the GMT time zone.

Finally, we print the output string, which should be "2021-10-04 01:30:00", representing the equivalent time in GMT.

gistlibby LogSnag