1

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?

amateur
  • 43,371
  • 65
  • 192
  • 320

1 Answers1

5

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.

Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks for this information. If I take it that I use utc datetimes everywhere, how could I convert it to the current timezone, based on the applications culture? – amateur Oct 04 '12 at 21:20
  • @amateur: You 'd need to get the user's local timezone (which is really independent of the culture, I could be Chinese but living in Paris) as a string and use the code I give. See also [`TimeZoneInfo.GetSystemTimeZones`](http://msdn.microsoft.com/en-us/library/system.timezoneinfo.getsystemtimezones.aspx). – Jon Oct 04 '12 at 21:23