TruMan1
TruMan1

Reputation: 36178

How to get timezone offset as ±hh:mm?

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

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

Upvotes: 31

Views: 19207

Answers (6)

Nikunj Kumbhani
Nikunj Kumbhani

Reputation: 3924

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

extension TimeZone {

    func offsetFromGMT() -> 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.offsetFromGMT()) // output is +0530
print(TimeZone.current.offsetInHours()) // output is "+05:30"

Upvotes: 11

Nataliia.dev
Nataliia.dev

Reputation: 2972

If you can use Date()

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

Will return "+01:00" format

Upvotes: 8

Kenrick Williams
Kenrick Williams

Reputation: 1

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)

Upvotes: 0

Bhavnish
Bhavnish

Reputation: 120

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.

Upvotes: 2

Akbar Khan
Akbar Khan

Reputation: 2415

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
    }
}

Upvotes: 0

Martin R
Martin R

Reputation: 540075

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.

Upvotes: 51

Related Questions