Setting/Changing an .NET application's TimeZone

回眸只為那壹抹淺笑 提交于 2020-01-14 19:22:28

问题


Is there a way to set a .NET application's TimeZone to a value other than the OS's TimeZone?

For instance, I am using my application on an OS with Central Standard Time set, and I would like the app to behave as if it were in Eastern Standard Time, without having to manually convert all DateTimes in my application using a method like TimeZoneInfo.ConvertTimeBySystemTimeZoneId().


回答1:


No, I don't believe there's any way of doing this. It's not like the time zone is set on a per thread basis like the culture. For instance, I believe that DateTime.ToLocalTime will always use the system time zone

However, I wouldn't use TimeZoneInfo.ConvertTimeBySystemTimeZoneId everywhere. I'd expose a property of type TimeZoneInfo which represented the appropriate time zone - or set up a helper class to convert where you need to.

Depending on your app, you may well find that if you choose the right points at which to convert the data (which isn't always a case of just keeping it in UTC everywhere apart from the UI layer) you don't have as many conversions to perform as you might expect.



来源:https://stackoverflow.com/questions/1120325/setting-changing-an-net-applications-timezone

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