Is there a way to set the local time, which may be different than the server's time, in an ASP.NET MVC3 application?

半腔热情 提交于 2019-12-04 05:19:58

问题


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?

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.


回答1:


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 is what you want.



来源:https://stackoverflow.com/questions/12255670/is-there-a-way-to-set-the-local-time-which-may-be-different-than-the-servers-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!