如何更改asp.net应用程序的时区 [英] How to change time zone for an asp.net application

查看:165
本文介绍了如何更改asp.net应用程序的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将ASP.NET的默认时区设置为亚洲/达卡或GMT + 6时区。但是我无法找到一种改变全球的方法。在Stackoverflow和其他网站上有很多参考,通过获取时区信息并计算每次我需要一个DateTime对象的正确时间来执行此操作。



但信任我,我不想这样做。所以没有给我任何这样的建议。我想将时区设置为亚洲/达卡或GMT + 6,最好是从web.config。 (与php.ini类似,在php中)所以每次我需要DateTime对象时,都会用时区进行评估,无论服务器的时区是什么。



这可能吗?如果可能的话呢?
提前感谢解决方案:)

解决方案

对不起,.NET中没有办法更改时区



您唯一的方法是更改​​服务器的时区或重写所有代码。



<最好的做法是不要依赖于系统时区(不要使用DateTime.Now)。



您应该将所有日期作为Utc日期处理,然后转换到一个特定的区域将其显示给用户。



即使您设法处理ASP.NET应用程序中的时区,SQL Server上仍然存在时区,例如GETTIME功能如果您的应用程序完全以UTC编写,那么您的SQL Server功能也可以正常工作。


I need to set default timezone for my ASP.NET to Asia/Dhaka or GMT+6 timezone. But i cannot find a way to change it globally. There is a lot of reference on Stackoverflow and rest of the web for doing this by getting timezone info and calculating correct time for each time i need a DateTime object.

But trust me, I don't want to do this in this way. So dont give me any suggestion like that. I want to set the timezone to Asia/Dhaka or GMT+6 preferably from web.config. (Similar we do in php with php.ini) So that each time i need DateTime object the time is evaluated with my timezone no matter what the timezone is for server.

Is this possible? If possible then how?? Thanks in advance for the solution :)

解决方案

Sorry there is no way in .NET to change the time zone globally.

The only way you have is to change the timezone of your server or rewrite all of your code.

The best practice is to not rely on the system time zone at all (never use DateTime.Now).

You should handle all date as Utc dates and then convert to a specific zone when displaying them to users.

Even if you manage to handle timezones in your ASP.NET application, there are still timezones on SQL Server, for example GETTIME funtion. If your application is entirely written in UTC, your SQL server function will work as well.

这篇关于如何更改asp.net应用程序的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆