23

I can get the offset seconds from GMT with this: TimeZone.current.secondsFromGMT().

However, how do I get the format as ±hh:mm?

TruMan1
  • 30,176
  • 50
  • 162
  • 301
  • Just get it using dateFormatter. `dateFormatter.dateFormat = "xxxxx"` – Leo Dabus Feb 14 '17 at 20:35
  • 1
    Possible duplicate of [Convert Seconds Integer To HH:MM, iPhone](http://stackoverflow.com/questions/1739383/convert-seconds-integer-to-hhmm-iphone) – rmaddy Feb 14 '17 at 20:36
  • @LeoDabus You can't achieve this with `DateFormatter`. There's no `Date`. – rmaddy Feb 14 '17 at 20:36
  • @rmaddy `dateFormatter.string(from: Date())` would return `"-02:00"` for me – Leo Dabus Feb 14 '17 at 20:37
  • @LeoDabus But there's no Date here, just a time zone. – rmaddy Feb 14 '17 at 20:38
  • @rmaddy all he needs to do is get it from current date. He can also set the dateFormatter to a different timeZone if he wants to. `TimeZone(abbreviation: "GMT")` would return "+00:00" – Leo Dabus Feb 14 '17 at 20:40

6 Answers6

42

Some integer arithmetic to obtain the offset in hours and minutes:

let seconds = TimeZone.current.secondsFromGMT()

let hours = seconds/3600
let minutes = abs(seconds/60) % 60

Formatted printing:

let tz = String(format: "%+.2d:%.2d", hours, minutes)
print(tz) // "+01:00" 

%.2d prints an integer with (at least) two decimal digits (and leading zero if necessary). %+.2d is the same but with a leading + sign for non-negative numbers.

Martin R
  • 510,973
  • 84
  • 1,183
  • 1,314
11

Here is extension for getting timezone offset Difference and as ±hh:mm (Swift 4 | Swift 5 Code)

extension TimeZone {

    func offsetFromUTC() -> String
    {
        let localTimeZoneFormatter = DateFormatter()
        localTimeZoneFormatter.timeZone = self
        localTimeZoneFormatter.dateFormat = "Z"
        return localTimeZoneFormatter.string(from: Date())
    }

    func offsetInHours() -> String
    {
    
        let hours = secondsFromGMT()/3600
        let minutes = abs(secondsFromGMT()/60) % 60
        let tz_hr = String(format: "%+.2d:%.2d", hours, minutes) // "+hh:mm"
        return tz_hr
    }
}

Use like this

print(TimeZone.current.offsetFromUTC()) // output is +0530
print(TimeZone.current.offsetInHours()) // output is "+05:30"
Nikunj Kumbhani
  • 3,432
  • 1
  • 21
  • 45
4

If you can use Date()

func getCurrentTimezone() -> String {
        let localTimeZoneFormatter = DateFormatter()
        localTimeZoneFormatter.dateFormat = "ZZZZZ"
        return localTimeZoneFormatter.string(from: Date())
    }

Will return "+01:00" format

Natali
  • 2,922
  • 4
  • 37
  • 52
2
extension TimeZone {
    
    func offsetFromUTC() -> String
    {
        let localTimeZoneFormatter = DateFormatter()
        localTimeZoneFormatter.timeZone = self
        localTimeZoneFormatter.dateFormat = "Z"
        return localTimeZoneFormatter.string(from: Date())
    }
    
 
    func currentTimezoneOffset() -> String {
      let timeZoneFormatter = DateFormatter()
      timeZoneFormatter.dateFormat = "ZZZZZ"
      return timeZoneFormatter.string(from: Date())
  }
}


Use like this

print(TimeZone.current.offsetFromUTC()) // output is +0530
print(TimeZone.current.currentTimezoneOffset()) // output is "+05:30"

it working 100% in all countries according to timezone.

Bhavnish
  • 46
  • 5
0

Swift 4 and above

extension TimeZone {

    func timeZoneOffsetInHours() -> Int {
        let seconds = secondsFromGMT()
        let hours = seconds/3600
        return hours
    }
    func timeZoneOffsetInMinutes() -> Int {
        let seconds = secondsFromGMT()
        let minutes = abs(seconds / 60)
        return minutes
    }
}
Akbar Khan
  • 1,976
  • 17
  • 24
0

The accepted answer does not handle the case "-00:30" correctly since the "+/-" is only being determined from the hours, and not the minutes. I would set the sign based on a check of the initial seconds value. Alternatively you could use DateComponentsFormatter.

    let formatter = DateComponentsFormatter()
    formatter.allowedUnits = [.hour, .minute, .second]
    formatter.unitsStyle = .positional
    formatter.zeroFormattingBehavior = .pad
    let interval: TimeInterval = TimeInterval.init(abs(secondsOffset))
    let offsetValue: String = formatter.string(from: interval)
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 16 '21 at 17:18