Is there a way to define the timezone for an application in ASP.NET such that all times read from/compared to current server time are implicitly converted, or do I need to put in conversion statements as each and every DateTime.Now call?
相关问题
- Carriage Return (ASCII chr 13) is missing from tex
- How to store image outside of the website's ro
- 'System.Threading.ThreadAbortException' in
- Request.PathInfo issues and XSS attacks
- How to dynamically load partial view Via jquery aj
相关文章
- asp.net HiddenField控件扩展问题
- asp.net HiddenField控件扩展问题
- Asp.Net网站无法写入错误日志,测试站点可以,正是站点不行
- asp.net mvc 重定向到vue hash字符串丢失
- FormsAuthenticationTicket expires too soon
- “Dynamic operations can only be performed in homog
- What is the best way to create a lock from a web a
- Add to htmlAttributes for custom ActionLink helper
I am not sure about the latest evolutions of ASP, but this article from 2006 provides an interesting answer:
What follows may not be need with .Net 3.5 and its TimeZoneInfo..ConvertTime method, but still, for completeness sake:
1) Get the stored timezone value for the user
2) Create a SimpleTimeZone class to wrap it (using some mapping scheme that maps a DB value to the underlying Windows registry version)
3) Use the SimpleTimeZone.ToLocalTime method to convert the DateTime value to the local time.
1) Get the stored timezone value from the user
2) Create a SimpleTimeZone class to wrap it
3) Use SimpleTimeZone.ToUniversalTime method to convert the DateTime to UTC.
Allow me to answer this question directly.
No, this is not possible. There is no mechanism in .NET to change the local time zone on a per-thread or per-application basis. It can only be changed system-wide by the time zone settings of the machine the code is running on. See this answer also.
You should not be using
DateTime.Now
in a web application. Because the time zone setting of your server can easily be changed, you should design your application to ignore it. That means usingDateTime.UtcNow
when reading the current system time. Alternatively, you can useDateTimeOffset
with itsDateTimeOffset.Now
orDateTimeOffset.UtcNow
properties.See also: The Case Against DateTime.Now
When you need to work with particular user's local time, then yes - you must put in conversions for each and every usage. You can use the
TimeZoneInfo
class to accomplish this.You also might consider using Noda Time, which offers an implementation of the IANA time zone database, and is a much better API in general.
Internally use DateTime.UtcNow. For .NET 3.5, check out TimeZoneInfo.ConvertTime.