Converting UTC DateTime to local DateTime

前端 未结 4 1636
终归单人心
终归单人心 2021-02-07 03:05

I have the following ASP.Net MVC Controller method:

public ActionResult DoSomething(DateTime utcDate)
{
   var localTime = utcDate.ToLocalTime();
}
4条回答
  •  被撕碎了的回忆
    2021-02-07 04:03

    Try the following:

    public static DateTime UtcToPacific(DateTime dateTime)
            {
                return TimeZoneInfo.ConvertTimeFromUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));
            }
    

    Obviously change Pacific to your local timezone.

    If you want to find out what time zones are present on your machine see the following link: http://msdn.microsoft.com/en-us/library/bb397781.aspx

提交回复
热议问题