【发布时间】:2013-04-27 02:05:04
【问题描述】:
我将所有 DateTime 字段存储为 UTC 时间。当用户请求网页时,我想采用他首选的本地时区(而不是服务器机器的本地时区)并自动将所有 Web 表单中的所有 DateTime 字段显示为本地日期。
当然,我可以在每个表单中的每个 DateTime.ToString() 调用上应用转换,或者实现一些辅助实用程序,但这是一项耗时的任务,而且还有一些难以使用自定义配置的第 3 方组件日期时间显示模板。
基本上,我想让 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。
【问题讨论】: