Amitesh
Amitesh

Reputation: 687

Different time output for ToUniversalTime

I cannot understand or find any information that could explain why there are two different time component output (12p.m and 11 a.m) for the following. Can somebody please explain.

        DateTime d1 = new DateTime(2015, 05, 15).ToUniversalTime();
        DateTime d2 = new DateTime(2015, 02, 02).ToUniversalTime();

        Console.WriteLine(d1.ToString()); //OUTPUTS - 1/05/2015 12:00:00 p.m.
        Console.WriteLine(d2.ToString()); //OUTPUTS - 1/02/2015 11:00:00 a.m.

Upvotes: 0

Views: 154

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241778

The ToUniveralTime method converts from the local time zone where the code is running, to UTC.

Since time zones can change their offsets from UTC at different times of the year, the value can easily be different between two different dates - especially since one date is in the winter, and the other is in the summer, due to daylight saving time.

See also, the DST tag wiki, and "time zone != offset" in the timezone tag wiki.

Upvotes: 1

Related Questions