title: How to include the timezone in serialized json datetime values from sql server table in Asp.Net webapi 2 app?
body: <p>I am using Visual Studio 2015 and I have an Asp.NET website application that uses webapi 2 to return data. The System.Web.Http.dll version is 5.2.30128.0. </p>
  
  <p>Lately, I came across this issue: <a href="https://stackoverflow.com/questions/15109894/new-date-works-differently-in-chrome-and-firefox">new Date() works differently in Chrome and Firefox</a>. To fix this particular issue I want to add the time zone to the serialized datetime values that come from the sql table. All the users of the app are in the same timezone. </p>
  
  <p>How can I achieve that across the entire app? </p>
  
  <p>The weird problem is that Date.Now or DateTime.Today are converted to json with time zone, however, the values that come from the database (I use sql server 2008R2 with datetime fields) don't have a timezone when converted to json.</p>
  
  <p>I created this sql table:</p>
  
  <pre><code>CREATE TABLE [dbo].[Entity]
    (
      [Id]                  INT IDENTITY (1, 1) NOT NULL
    , [Name]                NVARCHAR(50) NULL
    , [LastModifiedDate]    DATETIME NULL
    , [LastModifiedDate2]   DATETIME2(7) NULL
   )
  </code></pre>
  
  <p>I added it to EF 6 model:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  
  public partial class Entity
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public Nullable&lt;System.DateTime&gt; LastModifiedDate { get; set; }
      public Nullable&lt;System.DateTime&gt; LastModifiedDate2 { get; set; }
  }
  </code></pre>
  
  <p>and I have in the WebApi controller method:</p>
  
  <pre><code>public IEnumerable&lt;object&gt; Get()
  {
    //throw new Exception("This is my exception handler");
    //
    TestEntities dbctx = new TestEntities();
  
    Entity entity = new Entity {Id = 0, Name = "Test", LastModifiedDate = DateTime.Now, LastModifiedDate2 = DateTime.Today};
  
    var list = dbctx.Entities.OrderBy(x =&gt; x.Id).Take(1).ToList();
    list.Add(entity);
    return list;
  }
  </code></pre>
  
  <p>Json output:</p>
  
  <pre><code>[{"Id":1,"Name":"Entity 1111111","LastModifiedDate":"2016-06-05T17:34:28.787","LastModifiedDate2":"2016-06-05T17:38:14.547"}
  ,{"Id":0,"Name":"Test","LastModifiedDate":"2016-06-05T18:05:14.8194-07:00","LastModifiedDate2":"2016-06-05T00:00:00-07:00"}]
  </code></pre>
  
  <p>The data returned by the following query:</p>
  
  <pre><code>select top 1 Id, Name, LastModifiedDate, LastModifiedDate2
  from dbo.Entity
  order by 1
  </code></pre>
  
  <p>is:</p>
  
  <pre><code>  1 Entity 1111111  6/5/2016 17:34:28.7870000   6/5/2016 17:38:14.5470000
  </code></pre>
  
  <p>I came across this blog from 4 years ago: <a href="http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx" rel="nofollow noreferrer">http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx</a> but I was wondering if there is another way in the latest versions of the software. I haven't tried yet the solution proposed in the article.</p>
  
  <p>Update:</p>
  
  <p>I think it is because of the Kind property of the DateTime structure. The values that come from the database have Kind=Unspecified while LastModifiedDate and LastModifiedDate2 have Kind=Local.</p>