3

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)

Francesca
  • 26,842
  • 28
  • 90
  • 153
  • Maybe this answer helps: http://stackoverflow.com/questions/18553936/how-to-change-date-format-using-jquery-javscript – Streamside Jan 03 '14 at 09:52
  • you should take a look at moment.js, it's a library that helps you to manipulate and format time – Antoine Jan 03 '14 at 09:53

4 Answers4

9

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;
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
4
var date = new Date($.now());
var dateString = (date.getFullYear() + '-'
    + ('0' + (date.getMonth() + 1)).slice(-2)
    + '-' + ('0' + (date.getDate())).slice(-2));
console.log(dateString); //Will print "2015-09-18" when I wrote this comment

To explain, .slice(-2) gives us the last two characters of the string.

So no matter what, we can add "0" to the day or month, and just ask for the last two since those are always the two we want.

So if the MyDate.getMonth() returns 9, it will be:

("0" + "9") // Giving us "09"

so adding .slice(-2) on that gives us the last two characters which is:

("0" + "9").slice(-2)

"09"

But if date.getMonth() returns 10, it will be:

("0" + "10") // Giving us "010"

so adding .slice(-2) gives us the last two characters, or:

("0" + "10").slice(-2)

"10"
Ogglas
  • 62,132
  • 37
  • 328
  • 418
0

You can process this with Date object and get the format as per your need.

var yourdate = $item.find('pubDate').text();
// Above gives you date in String form. But for getting date, 
   first create in date object like below
var dt = new Date(yourdate);
var newDt = dt.getFullYear()+""+(dt.getMonth()+1)+""+dt.getDate();
// Output : 20131118
array += '<p>' + newDt + '</p>';

Here since getMonth() gives (0-11) month, so add 1.

For more details you can check : http://www.w3schools.com/jsref/jsref_obj_date.asp

Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
  • How does this fit into the code above? Sorry I am very new to all this. How can I take the data from pubDate as above and have that output in the "" where you have Mon, 18 etc – Francesca Jan 03 '14 at 09:59
  • @Francesca : updated the answer. Check for more details – Nishu Tayal Jan 03 '14 at 10:05
  • 1
    Imho, it omits a leading zero before months Jan thru Sep and days 1-9. It'll give 201599 instead of 20150909 for today, for example. – Leonard Sep 09 '15 at 07:25
0

Converting with DateJs should be as simple as:

<script type="text/javascript" src="date.js"></script>
<script type="text/javascript">
    var d1 = Date.parse('Mon, 18 Nov 2013 12:00:00 GMT');
    alert(d1.toString('dd/mm/yyyy HH:mm:ss'));
</script>
sas
  • 2,563
  • 1
  • 20
  • 28