Json Date

There is so many public API that using this kind of format in their JSON response.

CreatedOn: "/Date(1377996450274)/"

This is the way to convert back to readable datetime using javascript.

function toReadableDateTime(date)
{
   console.log(date);
   d = new Date(parseInt(date.replace(/\D/g, "")));
   return d.getFullYear() + "-" + Format(d.getMonth() + 1) + "-" + Format(d.getDate()) + " " + Format(d.getHours()) + ":" +       Format(d.getMinutes()) + ":" + Format(d.getSeconds());
}
function Format(num)
{
   num = "0" + num;
   return num.slice(-2);
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s