Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
391 views
in Technique[技术] by (71.8m points)

timezone - How do I enumerate all time zones in .NET?

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);
   }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...