开发者

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

开发者 https://www.devze.com 2023-02-06 07:12 出处:网络
I store dates as UTC, by calling DateTime.UtcNow when inserting new records. I wan开发者_JAVA百科t to convert that date to the visitor\'s timezone.

I store dates as UTC, by calling DateTime.UtcNow when inserting new records. I wan开发者_JAVA百科t 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号