0

I use the following code for getting the device timezone

func getCurrentTimeZone() -> String{

         return TimeZone.current.identifier

  }

  let currentTimeZone = getCurrentTimeZone()
   print(currentTimeZone)

It gives the location e.g. "Asia/Dhaka", but I want to get in the "+02:00 or -04:30" format. How can I convert the value to the desired format?

Y Junction
  • 181
  • 1
  • 15

1 Answers1

1

The desired format appears to be in ISO 8601 format, in which case you can use ISO8601DateFormatter:

let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withTimeZone, .withColonSeparatorInTimeZone]
dateFormatter.timeZone = TimeZone.current
let formattedTimezone = dateFormatter.string(from: Date())

Note that I have used Date() in the last line. This will output the offset from GMT of the timezone at the current instant. Replace this with a Date of your choice to get the offset from GMT of that timezone at that Date instead.

Sweeper
  • 213,210
  • 22
  • 193
  • 313