Time zone conversion in C#

前端 未结 2 702
既然无缘
既然无缘 2021-01-21 15:59

I have a date format, something similar to:

Mon, 11 Aug 2009 13:15:10 GMT

How do I convert this to EST format?

相关标签:
2条回答
  • 2021-01-21 16:42
    var datetime = DateTime.Parse("Sat, 21 Aug 2010 13:15:10 GMT");
    TimeZoneInfo estZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    DateTime estTime = TimeZoneInfo.ConvertTime(datetime, estZone);
    

    EST can mean different times, which do you want: http://en.wikipedia.org/wiki/EST

    0 讨论(0)
  • 2021-01-21 17:05

    This, or similar should do the trick:

    var dateString = "Tue, 11 Aug 2009 13:15:10 GMT";
    var date = Convert.ToDateTime(dateString);
    var result = TimeZoneInfo.ConvertTime(date, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));
    

    It's worth mentioning, that your originally specified Mon, 11 Aug 2009, is actually incorrect, hence I've changed it to Tue, 11 Aug 2009 so the code will run, as the Convert.ToDateTime throws an exception if the day does not match the date.

    I've also assumed that you mean Eastern Standard Time, which is the id associated with "Eastern Time (US & Canada)", but you can get a full list of the time zones available by running the following code:

    foreach(TimeZoneInfo info in TimeZoneInfo.GetSystemTimeZones())
    {
        Console.WriteLine("Id: {0}", info.Id);
        Console.WriteLine("   DisplayName: {0}", info.DisplayName);
    }
    
    0 讨论(0)
提交回复
热议问题