How do I enumerate all time zones in .NET?

前端 未结 4 1074
长发绾君心
长发绾君心 2020-12-05 02:58

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

相关标签:
4条回答
  • 2020-12-05 03:16

    If wanting a json output from a WebAPI call:

    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:

    [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"
      },...
    
    0 讨论(0)
  • 2020-12-05 03:20

    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

    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 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?

    0 讨论(0)
  • 2020-12-05 03:22

    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.

    0 讨论(0)
  • 2020-12-05 03:33

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

    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;
    }
    
    0 讨论(0)
提交回复
热议问题