convert gmt to australian eastern time in swift

To convert a GMT (Greenwich Mean Time) date to an Australian Eastern Time (AET) date in Swift, you would first create a DateFormatter object and set its time zone to GMT.

Then, you can create a Calendar object and set its time zone to the target time zone, in this case, the Australian Eastern Time zone.

Finally, you can use the Calendar object to convert the GMT date to the target time zone.

Here's some example code:

main.swift
// GMT date string
let gmtDateString = "2022-05-01 12:00:00"

// create date formatter, set time zone to GMT
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "GMT")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

// create date object from GMT date string
guard let date = dateFormatter.date(from: gmtDateString) else {
    fatalError("Failed to create date from string")
}

// create calendar object, set time zone to AET
var calendar = Calendar.current
calendar.timeZone = TimeZone(identifier: "Australia/Sydney")!

// convert GMT date to AET date
let aetDate = calendar.date(byAdding: .hour, value: 10, to: date)

// format AET date as string
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzz"
let aetDateString = dateFormatter.string(from: aetDate!)
print(aetDateString) // 2022-05-01 22:00:00 AEST
840 chars
25 lines

In this example, we're adding 10 hours to the GMT date to convert it to AET. This assumes that AET is currently 10 hours ahead of GMT. Be sure to adjust the value passed to date(byAdding:to:) based on the actual time difference between GMT and your target time zone.

gistlibby LogSnag