如何枚举 .NET 中的所有时区? [英] How do I enumerate all time zones in .NET?

查看:27
本文介绍了如何枚举 .NET 中的所有时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一份 Windows 机器上所有可用时区的列表.我怎样才能在 .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?

我知道 TimeZoneInfo.GetSystemTimeZones 方法,但这仅返回当前选择的时区

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);
   }
}

推荐答案

不,它没有,它返回 Windows 机器知道的每个时区(在我的安装中,这是 91).您拥有的 if 语句限制了您的输出.去掉它,但保留 Console.WriteLine 部分,您将看到所有 91 个(左右)时区.

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.

例如

ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();

foreach (TimeZoneInfo timeZoneInfo in timeZones)
  Console.WriteLine("{0}", timeZoneInfo.DisplayName);

这应该会向您的控制台写出 91 个时区.

That should write out 91 timezones to your console.

这篇关于如何枚举 .NET 中的所有时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆