WCF and JSON Date format

11th Oct 2011

If you have a WCF web service that allows the client to pass requests that contain JSON objects, you will at some point probably need to pass a date.  If your client is also written in C#, then you don't need to worry about this date format - the JavaScriptSerializer class will sort the date format out for you.

However, if your client is written in some other non .net language, then you may need to know what exactly the date is.

WCF dates are specified as the number of milliseconds since the 1st of January 1970. This is also sometimes referred to as an Epoch Time Value. The guys over at Esqsoft have a handy web based Epoch date time converter. Great for finding JSON date values to put into a request into Fiddler for testing.

So lets take today's date - 11th October 2011, and convert it to an Epoch Time Value:

1318287600

is our output value.

JSON Epoch ESQ

Lets see this in the context of a JSON request:

{
"LastUpdatedDate":"\\/Date(1318287600+0100)\\/",
}

As you can see, we need to wrap the Date in an escape sequence, and then a Date object. We also append the date with our time zone. In my case, I am appending a +0100 as I am one hour ahead of Greenwich Mean Time, currently on British Summer Time (although you wouldn't know it looking at the weather!)

Links

Bertrand Le Roy's blog post on WCF JSON dates http://weblogs.asp.net/bleroy/archive/2008/01/18/dates-and-json.aspx

Fiddler http://www.fiddler2.com/fiddler2/

ESQ Soft Epoch / Date Converter http://www.esqsoft.com/javascript_examples/date-to-epoch.htm

JavaScriptSerializer Class http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx