I have received a problem with converting the date format from Y-m-d H:i:s to dd-MM-YYYY by using JQUERY.
My json looks like:
{ "status": "ok", "posts": [ { "id": "21", "title": "Title", "date": "2013-06-26 06:46:29" } ]}
And ajax request:
$.ajax({ url: , async: false, callback: 'callback', crossDomain: true, contentType: 'application/json; charset=utf-8', dataType: 'jsonp', timeout: 2000, success: function (data, status) { if (data !== undefined && data.posts !== undefined) { $('#news').append('<a class="item" href="single.html?type=news&id=' + item.id + '">' + item.title + item.date + '</a>'); } } });
Can anyone kindly assist me by using jsfiddle? I am new to jquery….
Utilize the split
function in pure JavaScript, & toss the varaibles around.
var date = "2013-06-26 06:46:29";var dateSplit = date.split(" ");var dateSplit2 = dateSplit[0].split("-");var formattedDate = dateSplit2.reverse().join('-'); // 26-06-2013
This might seems a yet ugly, & it is. But it suits, as long as you don’t actually need to format the date in other ways (like timezone & such). Then you’d have to look into the Date()
object.
EDIT: I want to encourage people to use as much pure JavaScript as possible, due to it’s speed. Often jQuery libraries & functions is filled with overhead, which causes your site to not only load slow, yet moreover process slow. There are no short-cuts to the perfect code, you’ll have to spend some time with it, & learn some tips & tricks. Good luck 🙂
No comments:
Post a Comment