Is there a way in C# to get the timezone offset for a given Country and State? For example, if input is Country - "Canada" and State - "Saskatchewan", then output is "-06:00"?
Asked
Active
Viewed 973 times
0
-
Does this help you? https://stackoverflow.com/questions/19695439/get-the-default-timezone-for-a-country-via-cultureinfo – Damian Mar 15 '21 at 08:21
-
Thanks for your quick response, but I have already gone through this answer. It requires zone id information. I just have Country name and State Name. – vidhi Mar 15 '21 at 08:27
-
@vidhi Canada/Saskatchewan is a valid TZ database timezone name. – ProgrammingLlama Mar 15 '21 at 08:33
-
I think I misinterpreted the answer. It did help in finding the zone ids of different countries. Is there a way to find out the time offset from the zone id? – vidhi Mar 15 '21 at 11:21
1 Answers
0
As mentioned in the comments, modify the SO question's code like below to get latitude
and longitude
:
IEnumerable<string> zoneIds = TzdbDateTimeZoneSource.Default.ZoneLocations
.Where(x => x.CountryCode == countryCode)
.Select(x => new { x.Latitude, x.Longitude});
Then, one way is to use Azure Maps API to fetch Offset and Transition offsets:
GET https://atlas.microsoft.com/timezone/byCoordinates/json?subscription-key=[subscription-key]&api-version=1.0&options=all&query=47.0,-122
Sample response:
{
"Version": "2019a",
"ReferenceUtcTimestamp": "2019-06-17T22:16:59.0765556Z",
"TimeZones": [
{
"Id": "America/Los_Angeles",
"Aliases": [
"US/Pacific",
"US/Pacific-New"
],
"Countries": [
{
"Name": "United States",
"Code": "US"
}
],
"Names": {
"ISO6391LanguageCode": "en",
"Generic": "Pacific Time",
"Standard": "Pacific Standard Time",
"Daylight": "Pacific Daylight Time"
},
"ReferenceTime": {
"Tag": "PDT",
"StandardOffset": "-08:00:00",
"DaylightSavings": "01:00:00",
"WallTime": "2019-06-17T15:16:59.0765556-07:00",
"PosixTzValidYear": 2019,
"PosixTz": "PST+8PDT,M3.2.0,M11.1.0",
"Sunrise": "2019-06-17T05:12:21.267-07:00",
"Sunset": "2019-06-17T21:05:18.017-07:00"
},
"RepresentativePoint": {
"Latitude": 34.05222222222222,
"Longitude": -118.24277777777778
},
"TimeTransitions": [
{
"Tag": "PDT",
"StandardOffset": "-08:00:00",
"DaylightSavings": "01:00:00",
"UtcStart": "2019-03-10T10:00:00Z",
"UtcEnd": "2019-11-03T09:00:00Z"
},
{
"Tag": "PST",
"StandardOffset": "-08:00:00",
"DaylightSavings": "00:00:00",
"UtcStart": "2019-11-03T09:00:00Z",
"UtcEnd": "2020-03-08T10:00:00Z"
},
{
"Tag": "PDT",
"StandardOffset": "-08:00:00",
"DaylightSavings": "01:00:00",
"UtcStart": "2020-03-08T10:00:00Z",
"UtcEnd": "2020-11-01T09:00:00Z"
}
]
}
]
}
Secondly, you can use Google APIs to get Local time using latitude and longitude:
{
"Version": "2019a",
"ReferenceUtcTimestamp": "2019-06-17T22:16:59.0765556Z",
"TimeZones": [
{
"Id": "America/Los_Angeles",
"Aliases": [
"US/Pacific",
"US/Pacific-New"
],
"Countries": [
{
"Name": "United States",
"Code": "US"
}
],
"Names": {
"ISO6391LanguageCode": "en",
"Generic": "Pacific Time",
"Standard": "Pacific Standard Time",
"Daylight": "Pacific Daylight Time"
},
"ReferenceTime": {
"Tag": "PDT",
"StandardOffset": "-08:00:00",
"DaylightSavings": "01:00:00",
"WallTime": "2019-06-17T15:16:59.0765556-07:00",
"PosixTzValidYear": 2019,
"PosixTz": "PST+8PDT,M3.2.0,M11.1.0",
"Sunrise": "2019-06-17T05:12:21.267-07:00",
"Sunset": "2019-06-17T21:05:18.017-07:00"
},
"RepresentativePoint": {
"Latitude": 34.05222222222222,
"Longitude": -118.24277777777778
},
"TimeTransitions": [
{
"Tag": "PDT",
"StandardOffset": "-08:00:00",
"DaylightSavings": "01:00:00",
"UtcStart": "2019-03-10T10:00:00Z",
"UtcEnd": "2019-11-03T09:00:00Z"
},
{
"Tag": "PST",
"StandardOffset": "-08:00:00",
"DaylightSavings": "00:00:00",
"UtcStart": "2019-11-03T09:00:00Z",
"UtcEnd": "2020-03-08T10:00:00Z"
},
{
"Tag": "PDT",
"StandardOffset": "-08:00:00",
"DaylightSavings": "01:00:00",
"UtcStart": "2020-03-08T10:00:00Z",
"UtcEnd": "2020-11-01T09:00:00Z"
}
]
}
]
}

Nimantha
- 6,405
- 6
- 28
- 69

Harshita Singh
- 4,590
- 1
- 10
- 13