tags:

views:

295

answers:

3

Start with three variables, all are System.DateTime.

a: 10/2/2009 2:30:00 PM
b: 10/2/2009 2:30:00 PM
c: 10/2/2009 2:30:00 PM

Compare them to each other.

a=b: True
b=c: True
c=a: True

Ok, we have established that all three dates are equal. So when we convert them all to Universal time, we will get the same result. Right?

a.ToUniversalTime: 10/2/2009 9:30:00 PM
b.ToUniversalTime: 10/2/2009 9:30:00 PM
c.ToUniversalTime: 10/2/2009 2:30:00 PM

So what happened?

A: 

My bet is it has something to do with c being in a different timezone than a and b.

Aistina
Ah, but unlike DateTimeOffset, you can't store the timezone in a DateTime.
Jonathan Allen
+8  A: 

From http://msdn.microsoft.com/en-us/library/system.datetime.touniversaltime.aspx:

Starting with the .NET Framework version 2.0, the value returned by the ToUniversalTime method is determined by the Kind property of the current DateTime object.

  • Kind == Utc - no conversion performed
  • Kind == Local or Unspecified - conversion is performed according to the local timezone
Michael Burr
A: 

i agree, c is in different timezone.

    Dim a As DateTime = #10/2/2009 2:30:00 PM#
    Dim b As DateTime = #10/2/2009 2:30:00 PM#
    Dim c As DateTime = #10/2/2009 2:30:00 PM#

    If Not (a = b AndAlso b = c AndAlso c = a) Then Stop
    a = a.ToUniversalTime
    b = b.ToUniversalTime
    c = c.ToUniversalTime

    If Not (a = b AndAlso b = c AndAlso c = a) Then Stop
    'reach here no problem, so one of them was in a different timezone
dbasnett