convert australian central standard time to gmt in swift

To convert Australian Central Standard Time (ACST) to Greenwich Mean Time (GMT) in Swift, we need to create a DateFormatter and set the source and destination time zones.

Here's the code to convert a date string in ACST to GMT:

main.swift
let dateString = "2021-06-21 10:30:00"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.timeZone = TimeZone(identifier: "Australia/Adelaide")
let date = dateFormatter.date(from: dateString)

dateFormatter.timeZone = TimeZone(identifier: "GMT")
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let gmtDateString = dateFormatter.string(from: date!)
397 chars
10 lines

In this code, we first set the date string, which is in the format of "yyyy-MM-dd HH:mm:ss". We then create a DateFormatter and set the time zone to "Australia/Adelaide" - this is the time zone for ACST.

We use the dateFormatter to create a Date object from the dateString.

Next, we set the time zone of the dateFormatter to "GMT" and update the date format to match the original format. We then create a new string from the date object using these updated settings.

The resulting gmtDateString will be the original date and time in GMT.

gistlibby LogSnag