如何枚举.NET中的所有时区?

时间:2022-02-17 07:50:44

I would like to have a list of all the time zones available on a Windows Machine. How can I do this in .NET?

我想要一个Windows机器上所有可用时区的列表。我怎么能在.NET中这样做?

I know about the TimeZoneInfo.GetSystemTimeZones method, but this returns only the currently selected time zone(s)

我知道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);
   }
}

4 个解决方案

#1


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.

不,它没有,它返回Windows机器知道的每个时区(在我的安装中,那是91)。你在那里的if语句限制了你的输出。拿出来然后离开Console.WriteLine部分,你会看到所有91个(左右)时区。

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.

这应该写出91个时区到你的控制台。

#2


Your code works fine for me. Here's the output on my box:

你的代码对我来说很好。这是我的盒子上的输出:

You might be in the following time zones: (GMT) Casablanca (GMT)
Greenwich Mean Time : Dublin,
Edinburgh, Lisbon, London (GMT)
Monrovia, Reykjavik

您可能位于以下时区:(GMT)卡萨布兰卡(格林威治标准时间)格林威治标准时间:都柏林,爱丁堡,里斯本,伦敦(GMT)蒙罗维亚,雷克雅未克

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”部分,正如罗伯特所说。

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?

如果您认为您应该看到更多区域,您能否告诉我们您所在的时区,以便我们可以找出应该显示的其他区域?

#3


This method is used to bind all timezones in Dev express Drop Down. I hope it will help for Someone.

此方法用于绑定Dev express Drop Down中的所有时区。我希望这会对某人有所帮助。

private void FillTimeZone(ASPxComboBox ddlTimeZone)
{   
   ddlTimeZone.DataSource = TimeZoneInfo.GetSystemTimeZones();   
   ddlTimeZone.DataBind();  
   ListEditItem oListEditItem=new ListEditItem();   
   oListEditItem.Text=Helper.SELECT;   
   oListEditItem.Value=Helper.SELECT;   
   ddlTimeZone.Items.Add(oListEditItem);   
   ddlTimeZone.SelectedIndex = 0;
}

#4


If wanting a json output from a WebAPI call:

如果想要从WebAPI调用获得json输出:

using System;
using System.Collections.Generic;

namespace MyProject.ViewModels
{
    public class TimeZoneViewModel
    {
        public readonly List<CTimeZone> CTimeZones;

        public TimeZoneViewModel()
        {
            CTimeZones = new List<CTimeZone>();
            foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
            {
                var tz = new CTimeZone(z.Id, z.DisplayName, z.BaseUtcOffset);
                CTimeZones.Add(tz);
            }
        }

    }

    public class CTimeZone
    {
        public string Id { get; set; }
        public string DisplayName { get; set; }
        public TimeSpan BaseUtcOffset { get; set; }

        public CTimeZone(string id, string displayName, TimeSpan utcOffset)
        {
            Id = id;
            DisplayName = displayName;
            BaseUtcOffset = utcOffset;
        }
    }
}

Then use it in WebAPI:

然后在WebAPI中使用它:

[HttpGet("Api/TimeZones")]
public JsonResult GetTimeZones()
{
    return Json(new TimeZoneViewModel().CTimeZones);
}

Output:

[{
    "id": "Dateline Standard Time",
    "displayName": "(UTC-12:00) International Date Line West",
    "baseUtcOffset": "-12:00:00"
  },
  {
    "id": "UTC-11",
    "displayName": "(UTC-11:00) Coordinated Universal Time-11",
    "baseUtcOffset": "-11:00:00"
  },
  {
    "id": "Aleutian Standard Time",
    "displayName": "(UTC-10:00) Aleutian Islands",
    "baseUtcOffset": "-10:00:00"
  },
  {
    "id": "Hawaiian Standard Time",
    "displayName": "(UTC-10:00) Hawaii",
    "baseUtcOffset": "-10:00:00"
  },...

#1


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.

不,它没有,它返回Windows机器知道的每个时区(在我的安装中,那是91)。你在那里的if语句限制了你的输出。拿出来然后离开Console.WriteLine部分,你会看到所有91个(左右)时区。

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.

这应该写出91个时区到你的控制台。

#2


Your code works fine for me. Here's the output on my box:

你的代码对我来说很好。这是我的盒子上的输出:

You might be in the following time zones: (GMT) Casablanca (GMT)
Greenwich Mean Time : Dublin,
Edinburgh, Lisbon, London (GMT)
Monrovia, Reykjavik

您可能位于以下时区:(GMT)卡萨布兰卡(格林威治标准时间)格林威治标准时间:都柏林,爱丁堡,里斯本,伦敦(GMT)蒙罗维亚,雷克雅未克

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”部分,正如罗伯特所说。

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?

如果您认为您应该看到更多区域,您能否告诉我们您所在的时区,以便我们可以找出应该显示的其他区域?

#3


This method is used to bind all timezones in Dev express Drop Down. I hope it will help for Someone.

此方法用于绑定Dev express Drop Down中的所有时区。我希望这会对某人有所帮助。

private void FillTimeZone(ASPxComboBox ddlTimeZone)
{   
   ddlTimeZone.DataSource = TimeZoneInfo.GetSystemTimeZones();   
   ddlTimeZone.DataBind();  
   ListEditItem oListEditItem=new ListEditItem();   
   oListEditItem.Text=Helper.SELECT;   
   oListEditItem.Value=Helper.SELECT;   
   ddlTimeZone.Items.Add(oListEditItem);   
   ddlTimeZone.SelectedIndex = 0;
}

#4


If wanting a json output from a WebAPI call:

如果想要从WebAPI调用获得json输出:

using System;
using System.Collections.Generic;

namespace MyProject.ViewModels
{
    public class TimeZoneViewModel
    {
        public readonly List<CTimeZone> CTimeZones;

        public TimeZoneViewModel()
        {
            CTimeZones = new List<CTimeZone>();
            foreach (TimeZoneInfo z in TimeZoneInfo.GetSystemTimeZones())
            {
                var tz = new CTimeZone(z.Id, z.DisplayName, z.BaseUtcOffset);
                CTimeZones.Add(tz);
            }
        }

    }

    public class CTimeZone
    {
        public string Id { get; set; }
        public string DisplayName { get; set; }
        public TimeSpan BaseUtcOffset { get; set; }

        public CTimeZone(string id, string displayName, TimeSpan utcOffset)
        {
            Id = id;
            DisplayName = displayName;
            BaseUtcOffset = utcOffset;
        }
    }
}

Then use it in WebAPI:

然后在WebAPI中使用它:

[HttpGet("Api/TimeZones")]
public JsonResult GetTimeZones()
{
    return Json(new TimeZoneViewModel().CTimeZones);
}

Output:

[{
    "id": "Dateline Standard Time",
    "displayName": "(UTC-12:00) International Date Line West",
    "baseUtcOffset": "-12:00:00"
  },
  {
    "id": "UTC-11",
    "displayName": "(UTC-11:00) Coordinated Universal Time-11",
    "baseUtcOffset": "-11:00:00"
  },
  {
    "id": "Aleutian Standard Time",
    "displayName": "(UTC-10:00) Aleutian Islands",
    "baseUtcOffset": "-10:00:00"
  },
  {
    "id": "Hawaiian Standard Time",
    "displayName": "(UTC-10:00) Hawaii",
    "baseUtcOffset": "-10:00:00"
  },...