how to handle time zones and dates in multi country/lingual application

梦想与她 提交于 2019-12-08 12:52:37

问题


I am currently working on a c# web application with a backend sql server database. I have some issues around dates that I need to work out.

The application is hosted on web and database server in GMT time. The users are spread across 3 different time zones CEST, MSK, ALMT with the potential to have the site rolled out to other countries over time.

In code at present I use DateTime.Now and GETDATE() in SQL, and with the location of the servers, this is GMT.

My question is what form should the datestamps be stored in the database, when taking in to account the different cultures etc. Should it be utc dates? Of how should the following be handled? Is there a standard practice?


回答1:


Everything should be in UTC (not server's timezone; UTC) when created and shifted to the user's timezone just before being displayed.

To take UTC times, use DateTime.UtcNow on the web server and GETUTCDATE() on the database.

To convert DateTime values that specify UTC times to any other timezone:

var dt = DateTime.UtcNow;
var tz = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
var localTimeInNewYork = TimeZoneInfo.ConvertTimeFromUtc(dt, tz);

See also Converting Times Between Time Zones on MSDN.



来源:https://stackoverflow.com/questions/12735759/how-to-handle-time-zones-and-dates-in-multi-country-lingual-application

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