convert gmt to chatham time in swift

To convert a date from GMT to Chatham Time in Swift, you need to use the DateFormatter and TimeZone classes. Here's an example code snippet:

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

// Create a date object from a string in GMT time zone
let dateInGMT = dateFormatter.date(from: "2021-09-08 10:30:00")!

// Convert GMT time zone to Chatham time zone
let chathamTimeZone = TimeZone(identifier: "Pacific/Chatham")!
dateFormatter.timeZone = chathamTimeZone

// Format the date in Chatham time zone
let chathamTime = dateFormatter.string(from: dateInGMT)
534 chars
15 lines

In this example, we first create a DateFormatter with the date format and GMT time zone. We then create a date object from a string in GMT time zone. We create a TimeZone object for Chatham time zone and set the timeZone property of the dateFormatter to it.

Finally, we use the dateFormatter to format the date in the Chatham time zone and store it in the chathamTime constant.

Note that the string format and time zone identifiers may differ based on your specific requirements.

gistlibby LogSnag