convert gmt to australian central standard time in swift

To convert GMT to Australian Central Standard Time (ACST) in Swift, follow these steps:

  1. Create a DateFormatter object and set its time zone to GMT:
main.swift
let dateFormatter = DateFormatter()
dateFormatter.timeZone = TimeZone(identifier: "GMT")
89 chars
3 lines
  1. Set the input format of your GMT date string:
main.swift
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
49 chars
2 lines
  1. Parse your GMT date string using the dateFormatter:
main.swift
let gmtDate = dateFormatter.date(from: "2021-06-01 09:00:00")!
63 chars
2 lines
  1. Create another DateFormatter object and set its time zone to ACST:
main.swift
let acstDateFormatter = DateFormatter()
acstDateFormatter.timeZone = TimeZone(identifier: "Australia/Adelaide")
112 chars
3 lines
  1. Set the output format of your ACST date string:
main.swift
acstDateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
53 chars
2 lines
  1. Convert your GMT date to ACST date using the dateFormatter and convert it back to a string using the acstDateFormatter:
main.swift
let acstDateString = acstDateFormatter.string(from: gmtDate)
print(acstDateString) // Output: "2021-06-01 19:30:00"
116 chars
3 lines

This will allow you to convert a GMT date string to an Australian Central Standard Time (ACST) date string in Swift.

gistlibby LogSnag