views:

764

answers:

2

I need to store the datetime in CST timezone regardless of any timezone given.

The Clients who access the application are from from various time zones, like IST, CST, EST,...

I need to store all the datetime entered by the client in CST timezone to my database. And while retrieving, i need to convert back to there local timezone.

How to achieve it?

A: 

Something like this would most likely work out for you. I may be have an incorrect value for the time zone id but I think this is close. The timezone stuff is available in .NET 3.5+

DateTime clientDateTime = DateTime.Now;
        DateTime centralDateTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(clientDateTime, "Central Time (US & Canada)");

EDIT:

If you can store DateTime in UTC by detecting the client timezone whenever they provide you a date that would be the best option. Then depending on the client timezone you can render the date according to the client's local timezone.

Wil P
+2  A: 

It is generally accepted to store all datetime values in your DB in the GMT/UTC format.

For those who want to render the UTC value to a particular time zone for different users of the application, like wilpeck mentioned, it's suggested that you determine the end users locale and:

  • when persisting, store the locale with the UTC date value
  • when reading, render the UTC value to local time with the associated locale value

EDIT:

For example:

You might have a table with a field StartDateTime so to support multiple time zones you might have an additional field StartDateTimeOffset. If the client is in the Indian Standard Time (IST) zone you might have a datetime value of 2009/10/13 14:45 which is 2009/10/13 09:15 in UTC. So you would store the UTC value (2009/10/13 09:15) in the field StartDateTime and store the offset +05:30 in the StartDateTimeOffset field. Now when you read this value back from the DB you can use the offset to convert the UTC datetime value (2009/10/13 09:15) back to the local time of 2009/10/13 14:45.

cottsak
Can i have a sample for that? for ex.: Client's system is at IST and server is at CST.
Prasad
Edited to include example
cottsak
Thanks cottsak, it's very useful
Prasad