title: Proper Way to Convert JSON Date to .NET DateTime During Deserialization
body: <p>I have a javascript function that calls an MVC controller with JSON data:</p>
  
  <pre><code>var specsAsJson = JSON.stringify(specs);
  $.post('/Home/Save', { jsonData: specsAsJson });
  </code></pre>
  
  <p>On the server side, within the controller, I can't seem to get past this error:</p>
  
  <blockquote>
    <p>/Date(1347992529530)/ is not a valid value for DateTime.</p>
  </blockquote>
  
  <p>That exception happens when I call Deserialize() (third line in method below):</p>
  
  <pre><code>    public ActionResult Save(string jsonData)
      {
          var serializer = new JavaScriptSerializer();
          serializer.RegisterConverters(new[] { new TimeSpanJsonConverter() });
          var specs = serializer.Deserialize&lt;List&lt;EquipmentSpecWithParameterlessConstructor&gt;&gt;(jsonData);
  
          return View("Index", _allTrackerJobs);
      }
  </code></pre>
  
  <p>I've been doing some googling, and the above code is my latest attempt to make this work (using the TimeSpanJsonConverter from <a href="https://stackoverflow.com/a/12265196/279516">here</a>). Other approaches show sending only a date to the server, but I have a list of objects that have dates as some properties.</p>
  
  <p>Is there an elegant, generally-accepted approach to solving this, or do we still need some kind of ugly work-around? What's the right way to resolve this?</p>
  
  <p><strong>=================== End of original question ===================</strong></p>
  
  <hr>
  
  <p><strong>Edit - SOLVED by serializing using JsonConvert</strong></p>
  
  <p>See my <strong>answer</strong> below (not the crappy work-around in this question).</p>
  
  <hr>
  
  <p><strong>Edit - Crappy work-around</strong></p>
  
  <p>I created a DTO with the exact same fields as the domain object, except that I made the date fields strings so they would deserialize. Now that I can deserialize it, I'll work on getting the dates into a valid format so I can create domain objects from my DTOs.</p>
  
  <pre><code>public class EquipmentSpecDto
  {
      public string StartTime { get; set; }
      public string EndTime { get; set; }
      // more properties here
  }
  </code></pre>
  
  <p>And I simply just used the DTO for the deserialization:</p>
  
  <pre><code>var specs = serializer.Deserialize&lt;List&lt;EquipmentSpecDto&gt;&gt;(jsonData);
  </code></pre>
  
  <hr>
  
  <p><strong>Edit 2 - Converting JavaScript Dates to .NET</strong></p>
  
  <p>For completeness, and in the hopes that I save someone else an hour, this is how I was able to convert the javascript dates:</p>
  
  <pre><code>    foreach (EquipmentSpecDto specDto in specDtos)
      {
          // JavaScript uses the unix epoch of 1/1/1970. Note, it's important to call ToLocalTime()
          // after doing the time conversion, otherwise we'd have to deal with daylight savings hooey.
          DateTime unixEpoch       = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
          Double startMilliseconds = Convert.ToDouble(specDto.StartTime.Substring(6, 13));
          Double endMilliseconds   = Convert.ToDouble(specDto.EndTime.Substring(6, 13));
          DateTime startTime       = unixEpoch.AddMilliseconds(startMilliseconds).ToLocalTime();
          DateTime endTime         = unixEpoch.AddMilliseconds(endMilliseconds).ToLocalTime();
          EquipmentSpec spec       = new EquipmentSpec(startTime, endTime, specDto.Equipment);
  
          specs.Add(spec);
      }
  </code></pre>