Timezone conversion

  • Thread starter Thread starter Bijoy Naick
  • Start date Start date
B

Bijoy Naick

I have an events table which stores the time of each event - the time and
assoicated timezone. Is there a way of converting this time into GMT (with
support for DST).. some sort of function which accepts a time and timezone
as parameters and returns the time in GMT?
 
Microsoft recommends using UTC time (universal Time Coordinate)...similar to
the GMT concept. If you use that...

DateTime.ToLocalTime()
DateTime.ToUniversalTime()

If you want GMT, you'll have to write your own utility class.

-Jason
 
Yes they will. Let's say you need to convert the current time.

DateTime currentTime = DateTime.Now;
DateTime utcTime = currentTime.ToUniveralTime();

or the short version: DateTime utcTime = DateTime.Now.ToUniversalTime();

If you need to use a date other than "now":

DateTime myDate = DateTime.Parse("7/4/2004 13:00:00");
DateTime utcTime = myDate.ToUniversalTime();

the short version: DateTime utcTime = DateTime.Parse("7/4/2004
13:00:00").ToUniversalTime();

To convert back:

DateTime myDate = utcTime.ToLocalTime();

Of course, this example assumes you are using the timezone of the machine
the application lives on. If you're on the east cost and need to convert to
pacific, you can use the Calendar Object to set the locale and pull your
dates/times from that. Though, if you need that flexability, and are going
to bu reusing that code, I would take the custom utility class approach.
 
do u have any examples of the calendar object?

Jason Penniman said:
Yes they will. Let's say you need to convert the current time.

DateTime currentTime = DateTime.Now;
DateTime utcTime = currentTime.ToUniveralTime();

or the short version: DateTime utcTime = DateTime.Now.ToUniversalTime();

If you need to use a date other than "now":

DateTime myDate = DateTime.Parse("7/4/2004 13:00:00");
DateTime utcTime = myDate.ToUniversalTime();

the short version: DateTime utcTime = DateTime.Parse("7/4/2004
13:00:00").ToUniversalTime();

To convert back:

DateTime myDate = utcTime.ToLocalTime();

Of course, this example assumes you are using the timezone of the machine
the application lives on. If you're on the east cost and need to convert to
pacific, you can use the Calendar Object to set the locale and pull your
dates/times from that. Though, if you need that flexability, and are going
to bu reusing that code, I would take the custom utility class approach.
 
Sorry... in Java mode these days... The Calendar class in .Net doesn't allow
you to set the timezone. There is a TimeZone class, but it also uses the
timezone of the current computer.

Looks like, if you want that flexability, you're going to have to write you
own. You could Inherit the TimeZone class (DateTime is a structure and not
inheritable) and overload the ToLocalTime() method. That way, your new
class has all the other functionality you need to work with TimeZones and
UTC time.

Something like....

public DateTime ToLocalTime(DateTime dt, string tz) {
if (tz.ToLower().Equals("pst")) {
return dt.AddHours(-8);
}
}

Obviously, you'll have to get a litte more fancy to get around the daylight
savings time problem. You'll have to test it to get the math right, but you
can use the TimeZone.IsDaylightSavingTime method coupled with the
DaylightTime class to check if the date is within daylight savings time.
 
I've been using the ToUniversalTime object with great success until we moved
the clocks forward for daylight savins time. Now, ToUniversalTime shows UTC
+ 1 hour. How can I compensate for this automatically?

Brett
 
Back
Top