How to format a date in jQuery from full to YYYYMMDD?
casting, date, jquery
Solution
Looks like a valid date to me, so parse it with `new Date()`
var date = new Date('Mon, 18 Nov 2013 12:00:00 GMT');
FIDDLE
In your code, something like
ajax.done(function(xml){
var items = $(xml).find('item').map(function(){
var $item = $(this);
var array = '<div class="item">';
var date = new Date( $item.find('pubDate').text() );
var year = pad(date.getFullYear());
var month = pad(date.getMonth() + 1);
var day = pad(date.getDate());
var yyyymmdd = year + month + day;
array += '<p>' + yyyymmdd + '</p>';
return array;
});
function pad(numb) {
return (numb < 10 ? '0' : '') + numb;
}
Problem
I'm using AJAX to grab data from an XML file and print it back out to me. One of those is a date, and uses this code: ``` jQuery(function(){ $.ajax({ url: 'http://www.sagittarius-digital.com/news.rss', dataType: 'xml' }).done(function(xml){ var items = $(xml).find('item').map(function(){ var $item = $(this); var array = '<div class="item">'; array += '<p>' + $item.find('pubDate').text() + '</p>'; return array; ``` So the line: ``` array += '<p>' + $item.find('pubDate').text() + '</p>'; ``` brings back a date like this: ``` Mon, 18 Nov 2013 12:00:00 GMT ``` How can I cast this into YYYMMDD? Time is not required. Live version in action (big buggy, take a while to load)