title: Custom JavaScriptConverter for DateTime?
body: <p>I have an object, it has a DateTime property... I want to pass that object from an .ashx handler back to a webpage via AJAX/JSON... I don't want to use 3rd party controls... </p>
  
  <p>when I do this:</p>
  
  <pre><code>  new JavaScriptSerializer().Serialize(DateTime.Now);
  </code></pre>
  
  <p>I get this:</p>
  
  <pre><code>  "\/Date(1251385232334)\/"
  </code></pre>
  
  <p>but I want "8/26/2009" (nevermind localization... my app is very localized, so my date formatting assumptions are not up for debate in this question).  If I make/register a custom converter</p>
  
  <pre><code>public class DateTimeConverter : JavaScriptConverter
  {
      public override IEnumerable&lt;Type&gt; SupportedTypes
      {
          get { return new List&lt;Type&gt;() { typeof(DateTime), typeof(DateTime?) }; }
      }
  
      public override IDictionary&lt;string, object&gt; Serialize(object obj, JavaScriptSerializer serializer)
      {
          Dictionary&lt;string, object&gt; result = new Dictionary&lt;string, object&gt;();
          if (obj == null) return result;
          result["DateTime"] = ((DateTime)obj).ToShortDateString();
          return result;
      }
  
      public override object Deserialize(IDictionary&lt;string, object&gt; dictionary, Type type, JavaScriptSerializer serializer)
      {
          if (dictionary.ContainsKey("DateTime"))
              return new DateTime(long.Parse(dictionary["DateTime"].ToString()), DateTimeKind.Unspecified);
          return null;
      }
  }
  </code></pre>
  
  <p>then I get this result (since the return value of the custom serialize method is a dictionary):</p>
  
  <pre><code>{"DateTime":"8/27/2009"}
  </code></pre>
  
  <p>so now in my Javascript, instead of doing</p>
  
  <pre><code>somePerson.Birthday
  </code></pre>
  
  <p>I have to do</p>
  
  <pre><code>somePerson.Birthday.DateTime 
  
    or
  
  somePerson.Birthday["DateTime"]
  </code></pre>
  
  <p>how can I make the custom converter return a direct string so that I can have clean Javascript?</p>