title: JSON Date and DateTime serialisation in c# & newtonsoft
body: <p>We are sending JSON to an API defined by swagger that some properties are DateTime in the format yyyy-MM-ddThh:mm:ss.000Z (the milliseconds must be 3 digits or it fails validation at the endpoint) and some are Date (no time) properties.</p>
  
  <p>I have seen many messages saying use the formatters like this:</p>
  
  <pre><code>var jsonSettings = new JsonSerializerSettings();
  jsonSettings.DateFormatString = "yyyy-MM-ddThh:mm:ss.000Z"; //try .fffZ too
  var jsonObject= Newtonsoft.Json.JsonConvert.DeserializeObject&lt;OurSwaggerObject&gt;(json , setting);
  </code></pre>
  
  <p>but this does not convert the DateTimes into the correct format, and how does C# deal with a Date only type? It always seems to serialise as DateTime.MinValue()</p>
  
  <p>Here is an example:</p>
  
  <p>Someone sends me json as string but the the dates and datetimes in the incorrect format to be sent to the endpoint. I was hoping that the swagger class and json deserialisation would format them but it is not.</p>
  
  <p>This is the swagger generated class</p>
  
  <pre><code> public class OurSwaggerObject
      {
          [Newtonsoft.Json.JsonProperty("dateTimeField", Required = Newtonsoft.Json.Required.Always)]
          [System.ComponentModel.DataAnnotations.Required]
          [System.ComponentModel.DataAnnotations.RegularExpression(@"^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d\.\d{3}Z$")]
          public DateTime dateTimeField { get; set; }
  
          [Newtonsoft.Json.JsonProperty("dateField", Required = Newtonsoft.Json.Required.Always)]
          [System.ComponentModel.DataAnnotations.Required]
          [System.ComponentModel.DataAnnotations.RegularExpression(@"^\d{4}-\d\d-\d\d$")]
          public DateTime dateField { get; set; }
      }
  </code></pre>
  
  <p>So I try and coerce the json to be correct but I'm doing it wrong or something is missing</p>
  
  <pre><code>string json = @"{ 'dateTimeField': '1995-04-07T00:00:00',
                            'dateField': '1995-04-07T00:00:00'
                             }";
  
          /* The json we need to satisfy the swagger endpoint is:
  
            { 'dateTimeField': '1995-04-07T00:00:00.000Z',
              'dateField': '1995-04-07'
                             }              
            */
  
          OurSwaggerObject deserialisedIntoObject = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;OurSwaggerObject&gt;(json);
  
          string serialisedToString = Newtonsoft.Json.JsonConvert.SerializeObject(deserialisedIntoObject);
          //serialisedToString= "{\"dateTimeField\":\"1995-04-07T00:00:00\",\"dateField\":\"1995-04-07T00:00:00\"}"
  
          var jsonSettings = new JsonSerializerSettings();
          jsonSettings.DateFormatString = "yyyy-MM-ddThh:mm:ss.fffZ"; //this won't help much for the 'date' only field!
          deserialisedIntoObject = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;OurSwaggerObject&gt;(json,jsonSettings);
          serialisedToString = Newtonsoft.Json.JsonConvert.SerializeObject(deserialisedIntoObject, jsonSettings);
          //serialisedToString="{\"dateTimeField\":\"1995-04-07T00:00:00\",\"dateField\":\"1995-04-07T00:00:00\"}"
  </code></pre>