Reputation: 43
I am displaying the current time using this code:
let UTCDate = Date()
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm a"
formatter.timeZone = TimeZone(identifier:"GMT")
let defaultTimeZoneStr = formatter.string(from: UTCDate)
Can someone help me to check if this time is in which timezone(eg:central timezone, eastern timezone)....
Upvotes: 0
Views: 4259
Reputation: 8473
Unless the time is specified as a string with some kind of time zone indicator, such as "2017-04-14 10:00:00 EDT" or "2017-04-14 10:00:00 -0400", there's no way to tell what time zone for a given time value.
The Swift way to store times as Date
values, which simply specify a number of seconds before or after January 1, 2001, UTC, and to display any time value using the calendar and time zone that makes the most sense for the user. Usually, this means using the time zone settings, because in most cases -- but not all cases -- that time zone setting will match the time zone where the user is.
If you want the abbreviated name of the user's current time zone setting, use this:
Calendar.current.timeZone.abbreviation()! // returns "EDT" for me;
// I’m in the eastern time zone
// and on daylight saving time
Or if you prefer getting the time zone by geographic identifier, use this:
Calendar.current.timeZone.identifier // returns "America/New_York" for me
Or if you want the full name of the time zone, try this (and play with the parameters):
// Returns "Eastern Standard Time" for me
Calendar.current.timeZone.localizedName(for: .standard, locale: Locale.current)
Upvotes: 2