Help Converting CET Date time to GMT /UTC

K

Krish

I have the following code in C#,

DateTime myDate = new DateTime(2004,4,5,13,0,0);

String myString = myDate.ToUniversalTime().ToString();


-- I just realised that I did not provide the TimeZone for the date/time.
I would like to supply CET as the paramter, but I donot know where to do
this. I am working on converting datetime pairs from different timezones
to GMT. Really appreciate some help on this.

Regards
 
N

Nicholas Paldino [.NET/C# MVP]

Krish,

You will have to do this manually then. The ToUniversalTime method will
take the current time zone (as configured on the machine) and use that
offset.

Unfortunately, in a rare bout of inifinite wisdom that MS had, they
decided to not allow people to create instances of the TimeZone class (it's
abstract), or the derived class that the framework uses (it's internal).
Also, they didn't overload the ToUniversalTime method to allow an instance
of the TimeZone class to be passed to it in order to adjust the DateTime
instance accordingly.

So basically, you will have to figure out the offsets, daylight savings,
etc, etc, and modify the DateTime instance yourself (or use a whole lot of
reflection to get access to the appropriate classes).

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top