有没有办法在 ASP.NET MVC3 应用程序中设置本地时间,它可能与服务器的时间不同? [英] Is there a way to set the local time, which may be different than the server's time, in an ASP.NET MVC3 application?
问题描述
我有一个 MVC3 应用程序,它在本地显示当前时间,但在上传到托管时显示服务器时间.在 app.config 或 web.config 中是否有任何方法可以设置我的本地时间以在我的应用程序上传到服务器后显示?
I have an MVC3 application the displays the current time locally, but displays the servers time when uploaded to hosting. Is there any way in the app.config or the web.config that I can set my local time to display once my app is uploaded to the server?
更新:我正在寻找一种在全局范围内执行此操作的方法,而无需在每次引用 DateTime 时返回并更改代码.无法全局设置应用程序的时间似乎有点不切实际.
UPDATE:I'm looking for a way to do this globally without going back and changing the code for every time I reference DateTime. It seems a little impractical to not be able to set the time for the application globally.
推荐答案
在 web.config 中设置时区信息,并根据 web.config 的时区转换你的时间.
set timezone information in the web.config and convert your time according to the web.config's timezone.
DateTime timeUtc = DateTime.UtcNow;
TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(timezoneid); // timezoneid from web.config
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
cstTime 正是您想要的.
cstTime is what you want.
这篇关于有没有办法在 ASP.NET MVC3 应用程序中设置本地时间,它可能与服务器的时间不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!