TimeZoneInfo
does not provide abbreviation or a short name for a given Timezone. The only good way to do it is to have a dictionary that will map abbreviations to either Timezone.id
, StandardName
or DaylightName
properties. However, all the sources I searched for list of abbreviations have different timezone names, i.e. not the same as in Windows.
How do you show a user not a full name, id or any other name in .NET? I don't want the UtcOffset but Timezone abbreviation - PST for Pacific, UTC - for Universal, EST - for Eastern Standard and etc. Is there any C# compatible list or database with all possible timezones and their abbreviations that compatible with those that TimeZoneInfo.GetSystemTimeZones()
gives you?
From east to west they are Atlantic Standard Time (AST), Eastern Standard Time (EST), Central Standard Time (CST), Mountain Standard Time (MST), Pacific Standard Time (PST), Alaskan Standard Time (AKST), Hawaii-Aleutian Standard Time (HST), Samoa standard time (UTC-11) and Chamorro Standard Time (UTC+10).
Times zone abbreviations are not unique to a particular geographic location. As an example, the time zone abbreviation "IST" can refer to Irish Summer Time, Israel Standard Time or Indian Summer Time all of which observe different time zones..
UPDATED ANSWER
My original response is below, and is still valid. However there is now an easier way, using the TimeZoneNames library. After installing from Nuget, you can do the following:
string tzid = theTimeZoneInfo.Id; // example: "Eastern Standard time" string lang = CultureInfo.CurrentCulture.Name; // example: "en-US" var abbreviations = TZNames.GetAbbreviationsForTimeZone(tzid, lang);
The resulting object will have the properties similar to:
abbreviations.Generic == "ET" abbreviations.Standard == "EST" abbreviations.Daylight == "EDT"
You can also use this same library to get the fully localized names of the time zones. The library uses an embedded self-contained copy of the CLDR data.
ORIGINAL ANSWER
As others mentioned, Time zones abbreviations are ambiguous. But if you really want one for display, you need an IANA/Olson time zone database.
You can go from a Windows time zone to an IANA/Olson time zone and the other direction as well. But be aware that there could be multiple IANA/Olson zones for any given Windows zone. These mappings are maintained in the CLDR here.
NodaTime has both the database and the mappings. You can go from a .Net DateTime
or DateTimeOffset
with a TimeZoneInfo
, to a NodaTime Instant
and DateTimeZone
. From there, you can get the abbreviation name.
// starting with a .Net TimeZoneInfo var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); // You need to resolve to a specific instant in time - a noda Instant // For illustrative purposes, I'll start from a regular .Net UTC DateTime var dateTime = DateTime.UtcNow; var instant = Instant.FromDateTimeUtc(dateTime); // note that if we really wanted to just get the current instant, // it's better and easier to use the following: // var instant = SystemClock.Instance.Now; // Now let's map the Windows time zone to an IANA/Olson time zone, // using the CLDR mappings embedded in NodaTime. This will use // the *primary* mapping from the CLDR - that is, the ones marked // as "territory 001". // we need the NodaTime tzdb source. In NodaTime 1.1.0+: var tzdbSource = TzdbDateTimeZoneSource.Default; // in previous NodaTime releases: // var tzdbSource = new TzdbDateTimeZoneSource("NodaTime.TimeZones.Tzdb"); // map to the appropriate IANA/Olson tzid var tzid = tzdbSource.MapTimeZoneId(timeZoneInfo); // get a DateTimeZone from that id var dateTimeZone = DateTimeZoneProviders.Tzdb[tzid]; // Finally, let's figure out what the abbreviation is // for the instant and zone we have. // now get a ZoneInterval for the zone and the instant var zoneInterval = dateTimeZone.GetZoneInterval(instant); // finally, you can get the correct time zone abbreviation var abbreviation = zoneInterval.Name; // abbreviation will be either PST or PDT depending // on what instant was provided Debug.WriteLine(abbreviation);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With