convert gmt to australian central daylight time in swift

To convert GMT to Australian Central Daylight Time in Swift, you can use the following code:

main.swift
let date = Date() // create a Date object
let dateFormatter = DateFormatter() // create a DateFormatter object
dateFormatter.timeZone = TimeZone(identifier: "GMT") // set the time zone to GMT
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss" // set the date format
let gmtDate = dateFormatter.string(from: date) // format the Date object as a string in GMT

dateFormatter.timeZone = TimeZone(identifier: "Australia/Adelaide") // set the time zone to Australian Central Daylight Time
let acdtDate = dateFormatter.date(from: gmtDate) // convert the GMT date string to an Australian Central Daylight Time Date object

print(acdtDate!) // print the Australian Central Daylight Time Date object
689 chars
11 lines

This code creates a Date object using the current date and time. It then creates a DateFormatter object and sets the time zone to GMT. The code formats the Date object as a string using the specified date format and stores the result in the gmtDate constant.

The code then sets the time zone of the DateFormatter object to Australian Central Daylight Time and converts the gmtDate string to a Date object in that time zone. Finally, the code prints the acdtDate object.

Note: The identifier for Australian Central Daylight Time is "Australia/Adelaide", but you should use the identifier for the specific time zone you need to convert to if it is different.

gistlibby LogSnag