0

When I compile and run the following program targeting .NET Framework 3.5, the DateTime that's printed to the screen is 11/1/2006 7:05:00 PM. (I am in the Central time zone.) If I change my project to target .NET framework 4.0 or higher and run the program I get an output of 11/1/2006 6:05:00 PM, 1 hour earlier.

I've noticed that when using Framework 3.5 if I change my computer's checkbox for Daylight Saving Time the output changes to 6:05 PM, but when using Framework 4.x making changes to the Daylight Saving Time checkbox doesn't affect the output of the program.

What is going on here and which time is the "correct" time? Why would changing the targeted framework affect that?

using Newtonsoft.Json;
using System;

namespace Test
{
    public class MyData
    {
        public DateTime? ActivationDate { get; set; }
    }

    public class Program
    {
        public static void Main()
        {
            string json = "{ \"ActivationDate\":\"\\/Date(1162425900000-0400)\\/\"}";

            Console.WriteLine(JsonConvert.DeserializeObject<MyData>(json).ActivationDate);
        }
    }
}

I found this similar question (DateTime value is different across different versions of .NET framework) but the answer says that it is the locale settings and not the framework that are causing the issue. However, this doesn't seem to be in accordance with what I'm witnessing with my program wherein changing nothing but the framework (and reinstalling the Nuget package for Newtonsoft JSON) seems to be affecting the output.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
John Hodge
  • 1,645
  • 1
  • 13
  • 13
  • Maybe has some useful related info: https://stackoverflow.com/questions/10029099/datetime-parse2012-09-30t230000-0000000z-always-converts-to-datetimekind-l, particularly wrt .NET version differences. I assume Json.NET is using `DateTime`'s parse implementation, so this is really more a `DateTime` question than anything to do with serialization or Json.NET – Peter Duniho Aug 08 '16 at 21:26

1 Answers1

2

After digging through numerous websites looking for an answer I stumbled across https://blog.appliedis.com/2013/03/06/beware-daylight-saving-time-transitions-in-dot-net/ which pointed me in the right direction. As it turns out, .NET Framework 3.5 apparently didn't have a clear picture on when Daylight Saving Time ended in 2006 as evidenced by this test program:

using System;

namespace Test
{
    public class Program
    {
        public static void Main()
        {
            DateTime begin = new DateTime(2006, 10, 24);
            while (begin < new DateTime(2006, 12, 25))
            {
                Console.WriteLine(begin + " - " + begin.IsDaylightSavingTime());
                begin = begin.AddDays(1);
            }
        }
    }
}

Running this program compiled against Framework 3.5 gives the following results:

10/24/2006 12:00:00 AM - True
10/25/2006 12:00:00 AM - True
10/26/2006 12:00:00 AM - True
10/27/2006 12:00:00 AM - True
10/28/2006 12:00:00 AM - True
10/29/2006 12:00:00 AM - True
10/30/2006 12:00:00 AM - True
10/31/2006 12:00:00 AM - True
11/1/2006 12:00:00 AM - True
11/2/2006 12:00:00 AM - True
11/3/2006 12:00:00 AM - True
11/4/2006 12:00:00 AM - True
11/5/2006 12:00:00 AM - True
11/6/2006 12:00:00 AM - False
11/7/2006 12:00:00 AM - False
11/8/2006 12:00:00 AM - False
11/9/2006 12:00:00 AM - False
11/10/2006 12:00:00 AM - False
11/11/2006 12:00:00 AM - False

while running it against Framework 4.0 shows this:

10/24/2006 12:00:00 AM - True
10/25/2006 12:00:00 AM - True
10/26/2006 12:00:00 AM - True
10/27/2006 12:00:00 AM - True
10/28/2006 12:00:00 AM - True
10/29/2006 12:00:00 AM - True
10/30/2006 12:00:00 AM - False
10/31/2006 12:00:00 AM - False
11/1/2006 12:00:00 AM - False
11/2/2006 12:00:00 AM - False
11/3/2006 12:00:00 AM - False
11/4/2006 12:00:00 AM - False
11/5/2006 12:00:00 AM - False
11/6/2006 12:00:00 AM - False
11/7/2006 12:00:00 AM - False
11/8/2006 12:00:00 AM - False
11/9/2006 12:00:00 AM - False
11/10/2006 12:00:00 AM - False
11/11/2006 12:00:00 AM - False

At least it looks like Microsoft is aware of the issue. https://support.microsoft.com/en-us/kb/933509

John Hodge
  • 1,645
  • 1
  • 13
  • 13