convert gmt to australian eastern standard time in swift

To convert GMT to Australian Eastern Standard Time in Swift, you need to:

  1. Create a DateFormatter object and set it to the GMT timezone.
  2. Set the timeZone property of the DateFormatter object to the Australian Eastern Standard Time timezone.
  3. Use the date(from:) method of the DateFormatter object to convert the GMT date string to an NSDate object.
  4. Use the string(from:) method of the DateFormatter object to convert the NSDate object to an Australian Eastern Standard Time date string.

Here's an example code snippet:

main.swift
let dateString = "2021-09-01 12:00:00 GMT"

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: "GMT")

if let date = dateFormatter.date(from: dateString) {
    dateFormatter.timeZone = TimeZone(identifier: "Australia/Sydney")
    let convertedDateString = dateFormatter.string(from: date)
    print(convertedDateString) // Output: 2021-09-01 22:00:00
}
433 chars
12 lines

In this example, we're converting the GMT date string "2021-09-01 12:00:00 GMT" to Australian Eastern Standard Time. The output will be "2021-09-01 22:00:00", which is 10 hours ahead of GMT.

gistlibby LogSnag