Invitation Digital Tech Blog

Building Scalable & Responsive Architecture


Creating dates for JSON

We have a JSON-based API that I like to test using Fiddler. However, passing dates is always a pain. When using the .NET DataContractSerializer (or ServiceStack.Text’s default), they look like /Date(323481600000)/ and I never seem to have a quick way of converting a standard date into this format.

For example, to update a user, we’d send a PUT request with this request body:

  "FirstName": "Graham",
  "LastName": "Clark",
  "BirthDate": /Date(323481600000)/,
  "CountryCode": "gb",
  "Postcode": "BS1 4RW",
  "Gender": "Male",
  "Email": ""

The Date is the number of milliseconds since the UNIX Epoch (1st January, 1970). To get a UTC, you can do this in your browser’s Javascript console:

> var d = new Date(Date.UTC(1980, 1, 14));
> d.getTime();
< 319334400000

It wouldn’t be Javascript without something insane, and in this case it’s that the month is zero-indexed, so 1 is February.

I always forget exactly how to do this, so this post is mainly to provide a spot for a UI to get a UTC date in JSON format: