Converting UTC DateTime to local DateTime

前端 未结 4 1622
终归单人心
终归单人心 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 03:54

    as described by MSDN, you need the Kind property of your Date to be UTC, for the ToLocalTime() to work (and convert the date to local time. http://msdn.microsoft.com/en-us/library/system.datetime.tolocaltime.aspx

    Try this:

    utcDate = DateTime.SpecifyKind(utcDate, DateTimeKind.Utc);
    var localTime = utcDate.ToLocalTime();
    

提交回复
热议问题