Skip to content Skip to sidebar Skip to footer

Converting Datetime From Json To Readable Format

I'm getting besides other properties DateTime property which is rendered on the view as /Date(1346997005000)/ I should convert this to readable format as dd.mm.yy

Solution 1:

var dateString = "/Date(1346997005000)/";
var dx = newDate(parseInt(dateString.substr(6)));

var dd = dx.getDate();
var mm = dx.getMonth() + 1;
var yy = dx.getFullYear();

if (dd <= 9) {
    dd = "0" + dd;
}

if (mm <= 9) {
    mm = "0" + mm;
}

var displayDate = dd + "." + mm + "." + yy;

Use displayDate. If you have access to one of the numerous JavaScript date libraries (e.g. Moment.js), you should be able to just pass dx into a function and get the display string with one line of code. That'd be a nicer solution.

Solution 2:

Your timestamp is in milliseconds already, so just pass it to the date constructor like this to convert it to a Date object.

var d = newDate(unixtimestamp)

Then you can use the Date APIs to get parts of the date.

Post a Comment for "Converting Datetime From Json To Readable Format"