I have a web application that I'm writing (C#, MSSQL) and I need to store the timestamp when a record is stored in the system. Normally, I would just do this with SQL and the DATETIME function. However, the server is located in a different time zone than our company is located... AND we may change to another server in completely different time zone. The hosting company will not change the server time to match our local time zone. (Not that I blame them, but it was one thing I tried.)
So, my question is, what is the best way to store the date/time of the record update and what is the best way to present that date/time back to the user in our local time zone?
I want the least messy way of doing this, so a combo of C# and SQL would be fine as long as the solution is easy to implement. (My normal style is to do more work in stored procedures and less in C#, if that matters.)
Can you show me some sample code? Thanks!
Save UTC times in your database and localize those times every time you wanna show them to a user
always store DATETIME data in Universal Time Coordinated (UTC aka GMT)
C# DateTime provides DateTime.UtcNow and ToLocalTime(), SQL provides GETUTCDATE(). Here's a SQL function to convert UTC to local time -
example:
Store it as UTC time and then preform the time zone calculation on the client end in C# use .ToUniversalTime() on the DateTime object to get the UTC time, store that on the server and then use .ToLocalTime() to convert it back.