C#/ASP.NET MVC3, convert from DateTime.UtcNow, to client's timezone. How to get timezone info?

梦想与她 提交于 2019-12-21 17:17:58

问题


I store dates as UTC, by calling DateTime.UtcNow when inserting new records. I want to convert that date to the visitor's timezone.

There's a method on TimeZoneInfo to do it:

public static DateTime ConvertTimeFromUtc(
    DateTime dateTime,
    TimeZoneInfo destinationTimeZone
)

but where can I pull TimeZoneInfo destinationTimeZone from, in asp.net mvc3? Is it reliably available in the request object?


回答1:


Unfortunately it is not as straightforward as one would expect. The Request object will not give you the info you require. You will need to use javascript. Here is a good implementation: http://weblogs.asp.net/cprieto/archive/2010/01/03/handling-timezone-information-in-asp-net.aspx



来源:https://stackoverflow.com/questions/4789679/c-asp-net-mvc3-convert-from-datetime-utcnow-to-clients-timezone-how-to-get

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