Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enumerate all time zones in .NET?

Tags:

timezone

I would like to have a list of all the time zones available on a Windows Machine. How can I do this in .NET?

I know about the TimeZoneInfo.GetSystemTimeZones method, but this returns only the currently selected time zone(s)

DateTimeOffset current = DateTimeOffset.Now; ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones(); Console.WriteLine("You might be in the following time zones:"); foreach (TimeZoneInfo timeZoneInfo in timeZones) {    // Compare offset with offset for that date in that time zone    if (timeZoneInfo.GetUtcOffset(current).Equals(current.Offset))    {         Console.WriteLine("   {0}", timeZoneInfo.DisplayName);    } } 
like image 996
gyurisc Avatar asked Jan 14 '09 05:01

gyurisc


People also ask

How do you list times in multiple time zones?

Reference to a specific time and zone would follow standard guidelines with the zone in parentheses: 4:42 p.m. (PST), 11:03 a.m. (MDT), 2:30 p.m. (CST), 10:00 P.M. (EST). AP on the other hand advises to capitalize the full name of each time zone: Pacific/Mountain/Central/Eastern Standard Time.

How many time zones are there list?

The world is divided into 24 time zones.


1 Answers

No it doesn't, it returns every time zone the Windows machine knows about (in my installation, that's 91). The if statement you have there is what is limiting your output. Take that out but leave the Console.WriteLine part, and you'll see all 91 (or so) timezones.

e.g.

ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();  foreach (TimeZoneInfo timeZoneInfo in timeZones)   Console.WriteLine("{0}", timeZoneInfo.DisplayName); 

That should write out 91 timezones to your console.

like image 122
Robert C. Barth Avatar answered Sep 20 '22 16:09

Robert C. Barth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!