Wrestling All Day With WebAPI, NewtonSoft and JSON Date Formats

Posted by Peter Kellner on May 22, 2013 · 3 mins read

 

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
{

    public string MessageString { get; set; }
    public DateTime DateTimeRightNow { get; set; }
    public DateTime DateTime1PmOctober5Th2013 { get; set; }
}



public class DateTestController : ApiController
{

    public class LogEntry
    {
        public string Details { get; set; }
        public DateTime LogDate { get; set; }
    }

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

The results of when I run this are as follows:

image