简体   繁体   English

如何在多国/语言应用程序中处理时区和日期

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

I am currently working on ac# web application with a backend sql server database. 我目前正在使用带有后端sql服务器数据库的ac#Web应用程序。 I have some issues around dates that I need to work out. 我有一些需要解决的约会问题。

The application is hosted on web and database server in GMT time. 该应用程序在GMT时间托管在Web和数据库服务器上。 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. 用户分布在3个不同的时区CEST,MSK,ALMT,有可能随着时间的推移将该站点推广到其他国家。

In code at present I use DateTime.Now and GETDATE() in SQL, and with the location of the servers, this is GMT. 目前在代码中,我在SQL中使用DateTime.Now和GETDATE(),并随服务器位置一起使用,即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? 我的问题是,当考虑到不同的文化等时,日期戳应以什么形式存储在数据库中。应该是utc日期吗? Of how should the following be handled? 以下应如何处理? Is there a standard practice? 有标准做法吗?

Everything should be in UTC (not server's timezone; UTC) when created and shifted to the user's timezone just before being displayed. 创建后,所有内容都应使用UTC(而不是服务器的时区; UTC),并在显示之前将其转移到用户的时区。

To take UTC times, use DateTime.UtcNow on the web server and GETUTCDATE() on the database. 要获取UTC时间,请在Web服务器上使用DateTime.UtcNow ,在数据库上使用GETUTCDATE()

To convert DateTime values that specify UTC times to any other timezone: 要将指定UTC时间的DateTime值转换为任何其他时区:

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. 另请参见MSDN上的时区之间转换时间

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

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