将RSS pubdate字符串转换为jQuery中的时间和日期

时间:2013-05-08 11:52:44

标签: javascript jquery html rss pubdate

所以我有一个像Wed, 08 May 2013 11:11:30 GMT这样的字符串。 (仅供参考,它是从XML RSS文件中提取的)我想要的是从上面的字符串到两个变量Wed, 08 May11:11。据我所知,这里有几件事需要做:

  • 剪切第3个空格前的文字
  • 剪切第4个空格和第2个冒号之间的文本
  • 将这两段文字保存为变量

除此之外,我不知道该怎么办。有人可以帮忙吗?到目前为止我的代码如下:

$.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>\
                        ');
                    //}
                });
            }
        });

如您所见,我已经删除了标题,说明和阅读更多文字。 proxy.php文件只是一个file_get_contents请求。

2 个答案:

答案 0 :(得分:2)

问题的解决方案位于:Parse RSS pubDate to Date object in JavaScript

为了获得时间:

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);

答案 1 :(得分:0)

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 );

检查这是否完美,