0

I am building an aggregator that takes news stories from XML and outputs them as individual LI's.

I need to cast the data for the date (currently outputs as Mon, 16 Dec 2013 11:00:00 GMT) into a numerical format so that I can then run queries on the date.

For example I need to:

  • Select the 10 most recent items from the array
  • Store these in a sub-array
  • Randomise the sub-array and output

Essentially so the last 10 news stories are taken and then randomised.

The code so far is working here (takes 5 seconds to load) and is generated by:

<script>
      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 = '<li class="ourNewsItem">';
                array += '<a href="' + $item.find('link').text() + '">';
                array += '<h2>' + $item.find('title').text() + '</h2>';
                array += '<p>' + $item.find('description').text() + '</p>';
                array += '<p>' + $item.find('pubDate').text() + '</p>';
                array += '<p>Category: ' + $item.find('category').text() + '</p>';
                array += '</a>';
                array += '</li>';
                return array;
          }).get();
          $('ul').append(items.join(' '));
        }).fail(function(){
          console.log('error', arguments)
        })
      })
</script>
Francesca
  • 26,842
  • 28
  • 90
  • 153
  • item.find('pubDate').text is a string, so you need to convert it to a date type, then format that in the way you want it. Note YYYYMMDD would probably be a better choice than dd.mm.yy. – Tony Hopkinson Jan 02 '14 at 17:27
  • Do you have any examples of this being done? I am a bit new to this. Just looking for the correct way to cast the data. – Francesca Jan 02 '14 at 17:33
  • Jquery is not my thing, yet. No doubt someone will be along shortly – Tony Hopkinson Jan 02 '14 at 18:34
  • if you search on here for "jquery date", loads of stuff. Such as this. http://stackoverflow.com/questions/206384/how-to-format-a-json-date – Tony Hopkinson Jan 02 '14 at 18:39

0 Answers0