How to convert any local time to UTC

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

My application receives a date/time field and a timezone code such as "EDT"
from a database. I need to convert the date/time to UTC using the timezone.
The timezone can be from anywhere on the globe.

TIA.
 
Thanks, Nick.

I've actually gotten a little further than that. For example, there's a
ToUniversalTime method on the DateTime class which would do the trick if I
could tell it what time zone to use. Unfortunately, it just seems to assume
it is the local machine's time zone.

There is a TimeZone class, but I cannot figure out how to create an instance
based on a time zone other than the local machine's.

For instance, if I could write something like:

myDateTime.TimeZone = "CET"; //central european time
myDateTime = myDateTime.ToUniversalTime;

I'd be golden. But this is not how the stuff works apparently.

Based on the examples in MSDN and elsewhere, I can convert from my local
time to UTC and back to my heart's content, but converting Bangkok time to
UTC is not well covered.

Thanks for pitching in.
 
Can you change the timezone of the local machine to the one you want, do the
conversion, and then change it back again? I have no idea what side effects
this would cause (probably a bunch) but it might be a workaround.
 
Why couldn't you take the time as a local time, convert that to UTC and then
+- the given timezone (as int)?
 
Not supported in the framework according to this FAQ article:
What now?

There's a pretty good article on time zones on CodeProject - also
includes some code to get access to ALL timezones defined in your
Windows PC, and it also supports doing stuff like convert times from
EDT to UTC (universal) and then on to your target time zone.

http://www.codeproject.com/dotnet/WorldClock.asp

Marc
================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Thanks, Marc. That looks like a great solution. In the meantime, I've rolled
my own. But I'll look to use this in the next go round.

Thanks again.
 
Thanks, David, but the app is going to run unattended on a production server.

Marc S, two responses down, points to a good solution at CodeProject if
you're interested.
 
Yea, that's basically what I did. I was hoping there was an api I could use
rather than my own lookup for the offsets. Marc S, 1 response below, points
to a project at CodeProject that uses the registry.

Thanks.
 
Back
Top