MVC - UTC日期到LocalTime [英] MVC - UTC date to LocalTime

查看:115
本文介绍了MVC - UTC日期到LocalTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个MVC项目,我需要显示一个UTC日期转换为用户本地时间。在我的模型中,我通过了UTC日期,在视图中我正在尝试执行以下操作:

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:

<%: Html.DisplayFor(m=> m.SomeDate.ToLocalTime()) %>

这会引发异常。任何人都可以指出如何将UTC日期转换为本地日期时间的正确方向,以便在客户端显示。我们将存储日期为UTC,并且在显示时,这些日期将需要转换为本地计算机等价物。

This throws an exception. Can anybody point me in the correct direction on how to convert the UTC date to local datetime for display at customer end. We will be storing dates as UTC and at display time these dates will need to be converted to the local machine equivalent.

推荐答案

您将需要存储用户时区服务器端,然后使用这样的东西(尽管应该在控制器中完成,不是视图):

You will need to store the users timezone server side and then use something like this (although it should be done in the controller, not the view):

@TimeZoneInfo.ConvertTimeFromUtc(Model.CreatedOn, TimeZoneInfo.FindSystemTimeZoneById("E. Australia Standard Time"))

这篇关于MVC - UTC日期到LocalTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆