Convert RSS pubdate string to time and date in jQuery

5.1k views Asked by At

So I have a String which will be something like Wed, 08 May 2013 11:11:30 GMT. (FYI, it is pulled from an XML RSS file) What I am trying to to is go from the above string to two variables, Wed, 08 May and 11:11. As far as I can tell, there are several things that need to be done here:

  • Cut out the text before the 3rd space
  • Cut out the text between the 4th space and the 2nd colon
  • Save those two pieces of text as variables

Other than that, I am not sure what to do. Can anyone help? My code so far is as below:

$.ajax({
            type: 'GET',
            url: 'proxy.php',
            data: {requrl: "http://feeds.bbci.co.uk/news/rss.xml"},
            dataType: 'xml',
            success: function (xml) {
                $('#newsTimeline').empty();
                $(xml).find("item").each(function () {
                    //for(var i = 0; i < $item.length && i < 5; i++){
                        var title = $(this).find("title").text();
                        var description = $(this).find("description").text();
                        var linkUrl = $(this).find("link_url").text();
                        var link = "<a href='" + linkUrl + "' target='_blank'>Read More<a>";
                        $('#feedContainer').append('
                            <li>\
                                <time class="cbp_tmtime" datetime="2013-04-10 18:30"><span>4/10/13</span> <span>18:30</span></time>\
                                <div class="cbp_tmicon cbp_tmicon-phone"></div>\
                                <div class="cbp_tmlabel">\
                                    <h2>Ricebean black-eyed pea</h2>\
                                    <p>Winter purslane courgette pumpkin quandong komatsuna fennel green bean cucumber watercress. Pea sprouts wattle seed rutabaga okra yarrow cress avocado grape radish bush tomato ricebean black-eyed pea maize eggplant. Cabbage lentil cucumber chickpea sorrel gram garbanzo plantain lotus root bok choy squash cress potato summer purslane salsify fennel horseradish dulse. Winter purslane garbanzo artichoke broccoli lentil corn okra silver beet celery quandong. Plantain salad beetroot bunya nuts black-eyed pea collard greens radish water spinach gourd chicory prairie turnip avocado sierra leone bologi.</p>\
                                </div>\
                            </li>\
                        ');
                    //}
                });
            }
        });

As you can see, I am already stripping out the title, description and read more text. The proxy.php file is just a file_get_contents request.

2

There are 2 answers

4
Menelaos On BEST ANSWER

The solution to your problem is at: Parse RSS pubDate to Date object in JavaScript

In order to also get the hours:

var pubDate = "Sun, 27 Mar 2011 20:17:21 +0100";
var date = new Date(pubDate);

var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var string = date.getDate() + " " + months[date.getMonth()] + " " + date.getFullYear()

var time = date.getHours()+":"+date.getMinutes();

alert(string+"\n"+time);
0
Umesh Panchani On
var str1="Wed, 08 may 2013 11:11:30 GMT";
var test1 = str1.substring(0,str1.length-17);
alert(test1 );
var test2 = str1.substring(test1.length+4,str1.length-7);
alert(test2 );

check this is work perfectly,