Skip to content

Wrestling All Day With WebAPI, NewtonSoft and JSON Date Formats

Updated: at 11:18 PM

 

Half to document what I now believe and half to share my findings, I’m going to do a short post that shows the implications of changing the DateTimeKind parameter of a DateTime field in a WebAPI controller.  That is, a WebAPI controller can return it’s results as JSON and the data that is DateTime gets converted based on some rules that for me were not clear.

First, let me state my conclusion (for those with short attention spans), then I’ll show the data and include the visual studio 2012 project I used to make this.

DateTimeKind.Local 013-10-05T13:00:00-07:00 (Has Timezone)
DateTimeKind.Utc 2013-10-05T13:00:00Z (Has Z)
DateTimeKind.Unspecifed 2013-10-05T13:00:00 No Timezone or Z
Not specified (default) 2013-10-05T13:00:00 No Timezone or Z

 

In my case, if I left unspecified (or default) I get no timezone which my JavaScript library (SenchaTouch) seems to believe is UTC.  That throws my times off by 7 hours!

Just to be more clear, here is what I have for my WebAPI Controller:

public class DateTestRecord
{
<span class="kwrd">public</span> <span class="kwrd">string</span> MessageString { get; set; }
<span class="kwrd">public</span> DateTime DateTimeRightNow { get; set; }
<span class="kwrd">public</span> DateTime DateTime1PmOctober5Th2013 { get; set; }

}

public class DateTestController : ApiController {

<span class="kwrd">public</span> <span class="kwrd">class</span> LogEntry
{
    <span class="kwrd">public</span> <span class="kwrd">string</span> Details { get; set; }
    <span class="kwrd">public</span> DateTime LogDate { get; set; }
}

<span class="rem">// POST api/&lt;controller&gt;</span>
<span class="kwrd">public</span> HttpResponseMessage Get()
{
    <span class="kwrd">return</span> Request.CreateResponse(HttpStatusCode.OK, <span class="kwrd">new</span>
        {
            data = <span class="kwrd">new</span> List&lt;DateTestRecord&gt;()
                {
                    <span class="kwrd">new</span> DateTestRecord
                        {
                            MessageString = <span class="str">&quot;First Line&quot;</span>,
                            DateTime1PmOctober5Th2013 = <span class="kwrd">new</span> DateTime(2013, 10, 5, 13, 0, 0,DateTimeKind.Local),
                            DateTimeRightNow = DateTime.Now
                        },
                    <span class="kwrd">new</span> DateTestRecord
                        {
                            MessageString = <span class="str">&quot;Second Line&quot;</span>,
                            DateTime1PmOctober5Th2013 = <span class="kwrd">new</span> DateTime(2013, 10, 5, 13, 0, 0,DateTimeKind.Utc),
                            DateTimeRightNow = DateTime.Now
                        },
                    <span class="kwrd">new</span> DateTestRecord
                        {
                            MessageString = <span class="str">&quot;Third Line&quot;</span>,
                            DateTime1PmOctober5Th2013 = <span class="kwrd">new</span> DateTime(2013, 10, 5, 13, 0, 0,DateTimeKind.Unspecified),
                            DateTimeRightNow = DateTime.Now
                        },
                            <span class="kwrd">new</span> DateTestRecord
                        {
                            MessageString = <span class="str">&quot;Third Line&quot;</span>,
                            DateTime1PmOctober5Th2013 = <span class="kwrd">new</span> DateTime(2013, 10, 5, 13, 0, 0),
                            DateTimeRightNow = DateTime.Now
                        }
                },
            success = <span class="kwrd">true</span>,
            message = <span class="str">&quot;All Good&quot;</span>
        });
}

}

The results of when I run this are as follows:

image