0

I'm saving date time in CST timezone,how to change the CST date time to local time.

Ex:

In DB, Date time is 2013-01-21 06:50:00 and its timezone is CST.This Date Time should be converted into local current time.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
karthik
  • 33
  • 1
  • 4
  • Do you care about daylight saving time? – Oded Jan 21 '13 at 12:56
  • CST is not a timezone, it's an abbreviation, and it's ambiguous. Is it China Standard Time? Cuba Standard Time? Central Standard Time (US), Central Standard Time (Australia), etc. I hope you are storing more than just "CST" in your database. See [here](http://www.timeanddate.com/library/abbreviations/timezones/) for a full list of abbreviations. – Matt Johnson-Pint Jan 21 '13 at 16:23

2 Answers2

2

Save them as UTC time and then convert them to local time when loading to the UI.

dutzu
  • 3,883
  • 13
  • 19
0

A sample code would be like

using System;

public class Example
{
 public static void Main()
 {
  DateTime date1 = new DateTime(2010, 3, 14, 2, 30, 0, DateTimeKind.Local);
  Console.WriteLine("Invalid time: {0}", 
                    TimeZoneInfo.Local.IsInvalidTime(date1));
  DateTime utcDate1 = date1.ToUniversalTime();
  DateTime date2 = utcDate1.ToLocalTime();
  Console.WriteLine("{0} --> {1}", date1, date2);      
  }
 }
// The example displays the following output: 
//       Invalid time: True 
//   3/14/2010 2:30:00 AM --> 3/14/2010 3:30:00 AM

Hope this helps

Rohit
  • 10,056
  • 7
  • 50
  • 82