How to convert JSON date to JavaScript Date

I recently faced a problem while creating HTML table from JSON object list. The object list was generated by an ASP.Net web service and it had a Date field in it. On client side the date value was coming something like /Date(1383202800000)/. I want to show it in regular Human readable date format something like "Thu Oct 31 2013".

Here is the simple solution for this.
<script>
//Convert JSON field to date.
var d = new Date(parseInt(obj[prop].toString().replace("/Date(", "")));
//Check the type of converted object
if (Object.prototype.toString.call(d) === "[object Date]") {
             $("#idfield").text(d.toDateString());
}
</script>

I hope this solution will save you some valuable hours.

Subscribe for our monthly newsletter for updated articles and useful code scripts.

Share It

comments powered by Disqus
Chat With Us Close
Leave Message