Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8844103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:24:07+00:00 2026-06-14T11:24:07+00:00

If I save a .NET DateTime using System.Web.Script.Serialization.JavaScriptSerializer the deserialized version is an hour

  • 0

If I save a .NET DateTime using System.Web.Script.Serialization.JavaScriptSerializer the deserialized version is an hour different than the original version. Any ideas why?

EDIT: My workstation’s time zone is UTC.

A NUnit test is below; note that the assertion works only after adding an hour.

    [Test]
    public void JsonSerializationOfDateTimesDoesntWork()
    {
        var originalDateTime = new DateTime(2011, 6, 20, 6, 5, 4, 3);
        const string fileName = "C:\\temp\\testDateTime.json";
        using (var writer = new StreamWriter(fileName, false))
        {
            writer.Write(new JavaScriptSerializer().Serialize(originalDateTime));
        }
        DateTime newDateTime;
        using (var reader = new StreamReader(fileName, false))
        {
            var readToEnd = reader.ReadToEnd();
            newDateTime = new JavaScriptSerializer().Deserialize<DateTime>(readToEnd);
        }

        Assert.AreEqual(originalDateTime, newDateTime.AddHours(1)); // !!
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:24:08+00:00Added an answer on June 14, 2026 at 11:24 am

    The serializer apparently converts it to an instant in time, in the form of milliseconds since the unix epoch. In other words, it’s effectively calling ToUniversalTime() first.

    At that point, any information about the original “kind” of DateTime is lost.

    On deserialization, the result is always a DateTime of kind UTC.

    If you start with a DateTime with a kind of UTC, you’ll round-trip. If you need to remember the kind as well, you’ll need to keep that data separately. Just remember that local times can be inherently ambiguous.

    using System;
    using System.Web.Script.Serialization;
    
    class Test
    {
        public static void Main(string[] args)
        {
            var original = new DateTime(2011, 6, 20, 6, 5, 4, 3, DateTimeKind.Utc);
            var serializer = new JavaScriptSerializer();
            var text = serializer.Serialize(original);
            var parsed = serializer.Deserialize<DateTime>(text);
            Console.WriteLine("Original: {0} ({1})", original, original.Kind);
            Console.WriteLine("Text: {0}", text);
            Console.WriteLine("Parsed: {0} ({1})", parsed, parsed.Kind);
        }
    }
    

    Output:

    Original: 20/06/2011 06:05:04 (Utc)
    Text: "\/Date(1308549904003)\/"
    Parsed: 20/06/2011 06:05:04 (Utc)
    

    Of course, this just highlights the problems with DateTime being conceptually broken to start with…

    EDIT: Also, as noted in comments, I very much doubt that your workstation’s time zone is really UTC, by the way. I suspect it’s the UK time zone, which is UTC in winter, but UTC+1 in summer – and the date you’ve given is in summer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a asp.net web application and I'm using cache (HttpRuntime.Cache) to save some
I am using C# and want to save images using JPEG format. However .NET
I'm having problems with my ASP.NET web forms system. It worked on our test
I'm using .NET 4 EF and am trying to save the child entities (InvoiceLogs)
Is there a way in VB.net to save a Word document as a different
I am building an ASP.Net MVC 3 Web application using Entity Framework 4.1. To
I'm developing a WCF JSon Web service with C#, .NET Framework 4.0, using Entity
What is the easiest way to save content to excel from .net pages? I
In ASP.NET MVC, how can I create a single cookie and save multplie values
I have an asp.net ajax updatePanelAnimationExtender that is used to fade a save confirmation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.