Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting to and from datetime adds an hour?

I am writing a fairly large webapp in asp.net/c# with MSSQL 2008 r2 serving the database. The program needs to convert date/time strings (in ISO date format) to DateTime where they are used and later stored as smalldatetime in sql.

When the strings are converted to datetimes, an hour is mysteriously added to the result. I understand that being in the UK, we are subjected to daylight savings time (currently active) but surely the datetime.convert method understands this? When converting back to a string, the result is as expected.

I have written a small program to illustrate the problem (also including non 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 I get when I run 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

Is this the correct behavior? Am I being an idiot? I would have expected converting datetime to string and then the exact string back to datetime would give back the original input. If this is the correct behavior, any ideas on how to get a consistant result but still using Convert.ToDateTime()?

Many Thanks.

like image 712
Tom Broad Avatar asked Sep 13 '10 11:09

Tom Broad


People also ask

How do I use 24 hour format in DateTime?

Two 'H' in HH is for 24-hour format. Description as to why this works would be useful. Such as HH for 24 hour format as hh for 12 hour.

How do I convert DateTime to hours in Python?

to check if the difference is more than 6 hours you can just do abs(start_time - end_time) > timedelta(hours=6) .

How do you convert time to date and time?

Extract time only from datetime with formula 1. Select a blank cell, and type this formula =TIME(HOUR(A1),MINUTE(A1), SECOND(A1)) (A1 is the first cell of the list you want to extract time from), press Enter button and drag the fill handle to fill range.

What's the difference between DateTime and DateTimeOffset?

With its Kind property, DateTime is able to reflect only Coordinated Universal Time (UTC) and the system's local time zone. DateTimeOffset reflects a time's offset from UTC, but it does not reflect the actual time zone to which that offset belongs.


1 Answers

The 'Z' at the end of the datetime means "Zulu" (equivalent of UTC/GMT). By default when you convert a string with that at the end of it it will convert it to local time (+ 1 hour in your case).

Without the 'Z' .NET will assume the date is already in the correct format and not add the hour.

When you format the datetime back into a string you are using the format string of "U". This is telling .NET that it is a UTC time and should be formatted at such. Therefore it adds the 'Z' to the end. When you convert it back to a date time another hour is added to make it local.

To clarify:

d1: Starts as a UTC string -> local time (+ 1 hour) -> UTC string -> local time (+1 hour)

d2: Starts as a local string -> local time (no change) -> UTC string -> local time (+ 1 hour)

like image 65
Martin Harris Avatar answered Oct 21 '22 08:10

Martin Harris