Parse JSON Date

I currently use ASP.NET MVC for development on the server side and jQuery and assorted libraries on the client side. One annoying issue that keeps coming up because I keep forgetting about it is dates.

When the server returns JSON it encodes dates as a string, something like /Date(16847928349)/ when I would like to have it as a JavaScript date.

There are several solutions offered when I Googled it. On the client side the solution was mostly to parse it using some other kind of JSON parser like JSON2, or using a regular expression.

But I didn’t want to liter my client code conversion code every time I used a date. I want it to be automatic. So I decided to created a script that did what I wanted. It might be less efficient than parsing dates as needed, but it works seamlessly.

It works by replacing jQuery’s built in converter. Identifying dates using a regular expression and converting them. It does that by first using jQuery.parseJSON() then using JavaScript reflection to enumerate through arrays and properties.

When it finds a string it checks if it matches the ASP.NET MVC date format, if it does it converts it to a date.

To just just include the js file after including jquery.

Download jquery.DateTime.js

Leave a Reply

Your email address will not be published. Required fields are marked *