title: How do I get Newtonsoft to deserialize a date as it is and call it utc?
body: <p>I have a datetime in a sql database: 2020-07-21 13:55:22.990 This datetime is in UTC although(correct me if I'm wrong), there would be no way to know that within the confines of the database. For reference, I am in CST(UTC-6) and this question was written during daylight savings time (so I'm -5 from UTC at the moment).</p>
  <p>I'm sending a query to that database and getting back json using the following method (I don't believe there are any issues with it, but I've included it just in case):</p>
  <pre><code>private static string GetJSONFromSQLQuery(string query, string connectionString)
  {
      var dataTable = new DataTable();
  
      using (var sqlConnection = new SqlConnection(connectionString))
      using (var sqlCommand = new SqlCommand(query, sqlConnection))
      using (var sqlDataAdapter = new SqlDataAdapter(sqlCommand))
      {
          sqlConnection.Open();
          sqlDataAdapter.Fill(dataTable);
      }
  
      var rowsAsDictionaries = new List&lt;Dictionary&lt;string, object&gt;&gt;();
      foreach (DataRow row in dataTable.Rows)
      {
          var rowAsDictionary = new Dictionary&lt;string, object&gt;();
  
          // ReSharper disable once LoopCanBeConvertedToQuery
          foreach (DataColumn column in dataTable.Columns)
              rowAsDictionary.Add(column.ColumnName, row[column]);
  
          rowsAsDictionaries.Add(rowAsDictionary);
      }
  
      return new JavaScriptSerializer().Serialize(rowsAsDictionaries);
  }
  
  </code></pre>
  <p>This is a snippet of the json I get back (in a real call, there would be more properties in the object and generally more objects):</p>
  <pre><code>
  [
      {
          &quot;Timestamp&quot;:&quot;\\/Date(1595357722990)\\/&quot;,
      },
  ]
  </code></pre>
  <p><a href="https://www.freeformatter.com/epoch-timestamp-to-date-converter.html" rel="nofollow noreferrer">https://www.freeformatter.com/epoch-timestamp-to-date-converter.html</a> says this is 7/21/2020, 1:55:22 PM under 'Convert epoch timestamp to date'</p>
  <p>I then deserialize the json with newtonsoft:</p>
  <pre><code>var deserializedJson = JsonConvert.DeserializeObject&lt;List&lt;MyObjectType&gt;&gt;(json)
  </code></pre>
  <p>MyObjectType has a bunch of { get; set; } properties and one of them is:</p>
  <pre><code>public DateTime Timestamp { get; set; }
  </code></pre>
  <p>From the above json snippet and newtonsoft call, Timestamp would be set to 7/21, 6:55:22 and the Kind property would be set to Utc. This is <s>incorrect</s> not what I was expecting.</p>
  <p>After some searching I learned about the DateTimeZoneHandling property in JsonSerializerSettings and made the following calls with each of the 4 enum options:</p>
  <pre><code>var local = JsonConvert.DeserializeObject&lt;List&lt;MyObjectType&gt;&gt;(json, new JsonSerializerSettings
  {
      DateTimeZoneHandling = DateTimeZoneHandling.Local,
  
  });
  
  var roundtrip = JsonConvert.DeserializeObject&lt;List&lt;MyObjectType&gt;&gt;(json, new JsonSerializerSettings
  {
      DateTimeZoneHandling = DateTimeZoneHandling.RoundtripKind,
  
  });
  
  var unspecified = JsonConvert.DeserializeObject&lt;List&lt;MyObjectType&gt;&gt;(json, new JsonSerializerSettings
  {
      DateTimeZoneHandling = DateTimeZoneHandling.Unspecified,
  
  });
  
  var utc = JsonConvert.DeserializeObject&lt;List&lt;MyObjectType&gt;&gt;(json, new JsonSerializerSettings
  {
      DateTimeZoneHandling = DateTimeZoneHandling.Utc,
  
  });
  </code></pre>
  <p>Which gives me the following DateTime objects in MyObjectType (note these are snippets from a DateTime object):</p>
  <pre><code>deserializedJson:
      Time: 7/21 6:55:22,
      Kind: Utc
  
  local:
      Time: 7/21 1:55:22,
      Kind: Local
      
  roundtripKind:
      Time: 7/21 6:55:22,
      Kind: Utc
      
  unspecified:
      Time: 7/21 6:55:22,
      Kind: Unspecified
  
  utc:
      Time: 7/21 6:55:22,
      Kind: Utc
  </code></pre>
  <p>The Kind property is particularly important because I will be calling</p>
  <pre><code>TimeZoneInfo.ConvertTimeFromUtc(theDeserializedTimeStamp, TimeZoneInfo.Local);
  </code></pre>
  <h1>My desired output is:</h1>
  <pre><code>Time: 7/21 1:55:22,
  Kind: Utc
  </code></pre>
  <p>Since that would be reflective of what is in the database and I would be able to call TimeZoneInfo.ConvertTimeFromUtc to convert the UTC time to whatever the local timezone is.</p>
  <p>How do I get that output?</p>
  <h1>EDIT:</h1>
  <p>I found a workaround solution. I changed the return statement in GetJSONFromSQLQuery to:</p>
  <pre><code>return JsonConvert.SerializeObject(rowsAsDictionaries);
  </code></pre>
  <p>and all the DateTimeZoneHandling options left the time as 1:55 PM and had varying (but expected) results for the Kind property. Not sure why json from JavaScriptSerializer wouldn't play nicely, but I'm unstuck now at least.</p>
  <p>The json that JsonConvert.SerializeObject looks like this:</p>
  <pre><code>[
     {
        &quot;Timestamp&quot;:&quot;2020-07-21T13:55:22.99&quot;
     }
  ]
  </code></pre>