【发布时间】:2010-10-01 07:38:25
【问题描述】:
我想要一个 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);
}
}
【问题讨论】:
-
患有剪切-粘贴-炎? :) 您已经在遍历整个枚举,但是使用 if 语句过滤掉与当前枚举不同的偏移量。
-
我忽略了代码中的 if :(
标签: timezone