Created
December 14, 2018 18:22
-
-
Save harshvishu/32e7d03fdd462de1adc91fad9a21a39e to your computer and use it in GitHub Desktop.
UTC to local
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Date { | |
// Convert local time to UTC (or GMT) | |
func toGlobalTime() -> Date { | |
let timezone = TimeZone.current | |
let seconds = -TimeInterval(timezone.secondsFromGMT(for: self)) | |
return Date(timeInterval: seconds, since: self) | |
} | |
// Convert UTC (or GMT) to local time | |
func toLocalTime() -> Date { | |
let timezone = TimeZone.current | |
let seconds = TimeInterval(timezone.secondsFromGMT(for: self)) | |
return Date(timeInterval: seconds, since: self) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment