Skip to content

Instantly share code, notes, and snippets.

@harshvishu
Created December 14, 2018 18:22
Show Gist options
  • Save harshvishu/32e7d03fdd462de1adc91fad9a21a39e to your computer and use it in GitHub Desktop.
Save harshvishu/32e7d03fdd462de1adc91fad9a21a39e to your computer and use it in GitHub Desktop.
UTC to local
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