问题
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