tags:

views:

42

answers:

1

i have a datetime(in utc) saved in database and i also know the utc offset in the following format.

-03:00:00

how to convert this to a DateTime

A: 

This simplest way to apply an "offset" to a DateTime that you already have is to create a TimeSpan structure which holds your offset value, and then simply "add" the offset to the original DateTime value.

For example:

DateTime utcDateTime = DateTime.Parse("29 July 2010 14:13:45");
TimeSpan offSet = TimeSpan.Parse("-03:00:00");
DateTime newDateTime = utcDateTime + offSet;
Console.WriteLine(newDateTime);

This results in the following output:

29/07/2010 11:13:45

which is the original time (29 July 2010 14:13:45) minus 3 hours (the offset - -03:00:00).

Note that this technique is merely performing simple arithmetic with your DateTime value and does not take any time zones into account.

CraigTP