3

I'm from the UK, and have recently deployed a website on WinHost's Basic Package.

When using DateTime.Now() in C#, or GETDATE() in SQL, these are both returning something like GMT-8 (because the server is hosted in the US).

I think I'm a bit limited in terms of permissions on the server (for example I can't change my SQL Login Language).

What is the best method of storing these dates in GMT?

Curtis
  • 101,612
  • 66
  • 270
  • 352

3 Answers3

4

use DateTime.UtcNow and store as it is database. When reading from database assume UTC and convert into UK time using TimeZoneInfo class. Note that the SQL should not contain any information about time zone offsets.

AYK
  • 3,312
  • 1
  • 17
  • 30
3

In SqlServer use getUtcDate() to store all your datetime values. You can convert it to required timezone in your .NET application.

ajay swamy
  • 271
  • 1
  • 5
2

Description

I think you should store your DateTimes in UTC. Then you can simple convert it to another Timezone with the TimezoneInfo class.

  • Save DateTime.UtcNow to your database or use getUtcDate inside sql
  • Let the user choose his timezone and save them to to your database
  • Convert the UTC Time you have saved to the users timezone using TimeZoneInfo.ConvertTimeFromUtc

Sample

TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(/* destination timezone (users timezone)  */);
DateTime cstTime = TimeZoneInfo.ConvertTimeFromUtc(YourDateTimeFromDatabase, cstZone);

More Information

Community
  • 1
  • 1
dknaack
  • 60,192
  • 27
  • 155
  • 202
  • I've just looked up UTC, and it states `Gets a DateTime object that is set to the current date and time on this computer`. By "this computer", I'm assuming its referring to the shared server? – Curtis Apr 25 '12 at 08:54
  • right.. it reads the datetime of the server.. And since you read back the data from the same server, it should not be an issue. – ajay swamy Apr 25 '12 at 08:55
  • DateTime.UtcNow is the UTC time. It gets calculated in relation to the servers time. So if your server is up to date, .net can calculate the right utc. – dknaack Apr 25 '12 at 08:57