简体   繁体   English

C#/ ASP.NET MVC3,从DateTime.UtcNow转换为客户端的时区。 如何获得时区信息?

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

I store dates as UTC, by calling DateTime.UtcNow when inserting new records. 我通过在插入新记录时调用DateTime.UtcNow将日期存储为UTC。 I want to convert that date to the visitor's timezone. 我想将该日期转换为访问者的时区。

There's a method on TimeZoneInfo to do it: 在TimeZoneInfo上有一个方法可以做到这一点:

public static DateTime ConvertTimeFromUtc(
    DateTime dateTime,
    TimeZoneInfo destinationTimeZone
)

but where can I pull TimeZoneInfo destinationTimeZone from, in asp.net mvc3? 但是我在哪里可以从asp.net mvc3中拉出TimeZoneInfo destinationTimeZone 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. Request对象不会为您提供所需的信息。 You will need to use javascript. 你需要使用javascript。 Here is a good implementation: http://weblogs.asp.net/cprieto/archive/2010/01/03/handling-timezone-information-in-asp-net.aspx 这是一个很好的实现: http//weblogs.asp.net/cprieto/archive/2010/01/03/handling-timezone-information-in-asp-net.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM