title: Serializing .NET DateTime to Json string
body: <p>I'm writing a custom Json serializer to improve the perf of my service. And yes, I did consider Json.NET, servicestack, DataContractJsonSerializer, etc before I made the decision. My objects are flat with simple .NET types and I wanted to avoid the overhead of a much bigger library.</p>
  
  <p>Anyways, here is where I run into a small problem. I have the code to serialize DateTime -</p>
  
  <pre><code>    var epoch = new DateTime(1970, 1, 1, 0, 0, 0);
      sb.Append("\"\\/Date(");
      SerializeNumber((time - epoch).TotalMilliseconds, sb);
      sb.Append(")\\/\"");
  </code></pre>
  
  <p>And this works great, except I can't quite get it to match the default .NET Json serializer in the output.</p>
  
  <pre><code>.NET serializer 
  "\\/Date(1328057884253)\\/\"
  Custom serializer
  "\\/Date(1328057884253.04)\\/\"
  </code></pre>
  
  <p>Hmm, so I tried making my conversion less precise and switching to (int)TotalSeconds instead of milliseconds and that gives me this -</p>
  
  <pre><code>.NET serializer
  "\\/Date(1328054810067)\\/\"
  Custom serializer
  "\\/Date(1328054810)\\/\"
  </code></pre>
  
  <p>I'm guessing that this wouldn't be a big deal but it would be nice to get my unit tests passing against the default .NET serializer just for sanity. Any ideas?</p>
  
  <p>Thanks.</p>