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);
}
}
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 theConsole.WriteLine
part, and you'll see all 91 (or so) timezones.e.g.
That should write out 91 timezones to your console.
Your code works fine for me. Here's the output on my box:
That's all the ones with the same offset at the moment, which is what your code clearly displays - if you want all the timezones, just remove the "if" part, as Robert says.
If you think you should be seeing more zones, could you tell us which timezone you're in so we can work out what other ones should be displayed?
If wanting a json output from a WebAPI call:
Then use it in WebAPI:
Output:
This method is used to bind all timezones in Dev express Drop Down. I hope it will help for Someone.