我想有一个Windows机器上所有可用时区的列表.我怎么能在.NET中这样做?
我知道TimeZoneInfo.GetSystemTimeZones方法,但这只返回当前选定的时区
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);
}
}
Run Code Online (Sandbox Code Playgroud)
Rob*_*rth 42
不,它没有,它返回Windows机器知道的每个时区(在我的安装中,那是91).if
你在那里的陈述是限制你的输出.拿出来然后离开Console.WriteLine
部分,你会看到所有91个(左右)时区.
例如
ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();
foreach (TimeZoneInfo timeZoneInfo in timeZones)
Console.WriteLine("{0}", timeZoneInfo.DisplayName);
Run Code Online (Sandbox Code Playgroud)
这应该写出91个时区到你的控制台.
你的代码对我来说很好。这是我的盒子上的输出:
您可能位于以下时区: (GMT) 卡萨布兰卡 (GMT)
格林威治标准时间:都柏林、
爱丁堡、里斯本、伦敦 (GMT)
蒙罗维亚、雷克雅未克
这是目前具有相同偏移量的所有那些,这就是您的代码清楚地显示的内容 - 如果您想要所有时区,只需删除“if”部分,正如罗伯特所说。
如果您认为应该看到更多时区,能否告诉我们您所在的时区,以便我们确定应该显示哪些其他时区?
归档时间: |
|
查看次数: |
12863 次 |
最近记录: |