MVC - UTC date to LocalTime

前端 未结 9 566
春和景丽
春和景丽 2020-12-28 10:07

We have a MVC project and I need to display a UTC date converted to users local time. In my model I am passing the UTC date and in the view I am trying to do the following:<

9条回答
  •  独厮守ぢ
    2020-12-28 10:56

    In a .NET Core project, I finally had success with the following razor code:

    @Model.DateUpdated.ToLocalTime().ToString(
                      CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern 
                      + " " + 
                      CultureInfo.CurrentUICulture.DateTimeFormat.LongTimePattern)
    

    (Inspiration taken from Kelly R's answer)

提交回复
热议问题