convert gmt to central time in swift

To convert GMT to Central Time in Swift, you can use the DateFormatter class to format the date from GMT to Central Time. Here's an example code snippet:

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

let gmtDate = dateFormatter.date(from: "2021-01-01 00:00:00")

dateFormatter.timeZone = TimeZone(identifier: "America/Chicago")
let centralTime = dateFormatter.string(from: gmtDate ?? Date())
331 chars
9 lines

In the code above, we first create a DateFormatter object and set its dateFormat to the format of the date string we want to convert. We then set the time zone of the date formatter to GMT and parse the input date string into a Date object.

Next, we set the time zone of the date formatter to Central Time and use it to format the Date object into a new string in the desired time zone.

Note that we've used "America/Chicago" as the identifier for Central Time, which is one of the time zones in the central time zone in the United States. You can choose a different identifier based on your requirements.

gistlibby LogSnag