Web Develop Wolf
Web Develop Wolf

Reputation: 6346

JSON Date Formatting

I'm having an issue with the date format when getting JSON Date/Time.

I've managed to get the date format to: 2009-06-25T17:32:10.0000000

But I need to be: 25/06/2009 17:32:10

But since I'm getting this data from a loop I've trouble finding out how exactly to format the date so that the data is still extracted correctly in the loop. So far the closest I've come is using the Jquery FormatDateTime (which I've added a script Reference for at the start of the document):

//Get Pages Visited
        response.Result.PagesViewed.forEach(function (o, i, arr) {
            o.PageTime = $formatDateTime('dd/mm/yy hh:ii', o.PageTime);
            $('#PagesViewedRows').html("<tr class='info'><td>" + o.PageTime + "</td><td>" + o.PageStatus + "</td><td>" + o.PageName + "</td></tr>");
});

How can I format the o.PageTime to display correctly?

Upvotes: 0

Views: 100

Answers (2)

guest271314
guest271314

Reputation: 1

Try

var d = new Date().toJSON().split(/-|T|\..*/).filter(Boolean);
var res = d.slice(0, 3).reverse().join("/").concat(" " + d.slice(-1));
console.log(res);

Upvotes: 0

ben
ben

Reputation: 3568

You don't really need JQuery for that. Plain javascript works as well:

var date = new Date('2009-06-25T17:32:10.0000000');
console.log(date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear() + ' ' + date.getHours() + ':' + date.getMinutes() + ':' + date.getSeconds());

Upvotes: 1

Related Questions