我将所有DateTime字段存储为UTC时间。当用户请求网页时,我想采用他首选的本地时区(而不是服务器本地时区),并自动将所有Web表单中的所有DateTime字段显示为本地日期。将UTC日期时间全局转换为用户指定的本地日期时间
当然,我可以在每个窗体的每个DateTime.ToString()调用上应用转换,或者实现一些辅助工具,但这是一个耗时的任务,还有一些第三方组件很难配置自定义DateTime显示模板。
从本质上讲,我想提出DateTime类的行为如下:
from this moment on for this web request,
whenever some code calls DateTime.ToString(), convert it to the local time
using the timezone offset given at the very beginning of the web request,
but if possible, please keep .NET core library DateTime.ToString() calls intact
(I don't want to mess up event logging timestamps etc.)
有没有办法做到这一点?
顺便说一句,我使用ASP.NET MVC 4,如果它很重要。
很好的答案!你说这也可以用JavaScript中的内置Date对象完成。你能否更新你的答案以表明如何做到这一点? – Scott 2013-12-31 17:05:17
@Scott - 已更新。谢谢。 – 2013-12-31 18:04:20