Converting to and from datetime adds an hour?

I am writing a fairly large webapp in asp.net/c#c MSSQL 2008 r2serving a database. The program needs to convert the strings date/time(in ISO date format) to DateTimewhere they are used and then saved as smalldatetimeto sql.

When strings are converted to datetimes, the result is mysteriously added hour. I understand that, while in the UK, we are exposed to daylight saving time (currently active), but, of course, DateTimedoes the .convert method understand this? When converting back to a string, the result will be as expected.

I wrote a small program to illustrate the problem (also including the lack of ISO dates for my sanity):

class Program
{


    static void Main(string[] args)
    {
        //BB();
        //Dist();
        DateTime d1 = new DateTime();
        DateTime d2 = new DateTime();
        string d1s = "2010-09-13T09:30:01Z";
        string d2s = "2010-09-13 09:30:01";

        d1 = Convert.ToDateTime(d1s);
        d2 = Convert.ToDateTime(d2s);

        Console.WriteLine("d1s:{0} d1:{1} ", d1s, d1);
        Console.WriteLine("d2s:{0} d2:{1} ", d2s, d2);

        d1s = d1.ToString("u"); d2s = d2.ToString("u");

        Console.WriteLine("\nd1: {0}", d1s);
        Console.WriteLine("d2: {0}", d2s);

        d1 = Convert.ToDateTime(d1s);
        d2 = Convert.ToDateTime(d2s);

        Console.WriteLine("\nd1s:{0} d1:{1} ", d1s, d1);
        Console.WriteLine("d2s:{0} d2:{1} ", d2s, d2);

        Console.Read();
    }
}

Here are the results that I get when I start the program:

d1s:2010-09-13T09:30:01Z d1:13/09/2010 10:30:01
d2s:2010-09-13 09:30:01 d2:13/09/2010 09:30:01

d1: 2010-09-13 10:30:01Z
d2: 2010-09-13 09:30:01Z

d1s:2010-09-13 10:30:01Z d1:13/09/2010 11:30:01
d2s:2010-09-13 09:30:01Z d2:13/09/2010 10:30:01
Done

? ? , datetime . , , , Convert.ToDateTime()?

.

+5
1

"Z" datetime "Zulu" ( UTC/GMT). , , (+ 1 ).

"Z" .NET , .

datetime , "U". .NET, UTC . "Z" . , , .

:

d1: UTC → (+ 1 ) → UTC → (+1 )

d2: → ( ) → UTC → (+ 1 )

+11

All Articles