如何使用 RSS2JSON 显示和格式化日期?
How to show and format date with RSS2JSON?
我无法在每个 RSS 项目标题下显示日期。日期显示,但它们只显示在我页面的底部。我一直在反复试验,但在这一点上我被困了一段时间。
<script type="text/javascript">
var content = document.getElementById('insurance-news');
function showFeed(data){
if(data.status == 'ok'){
var feedlength = data.items.length;
var output = '';
function formatDate(myElement){
var date = new Date(myElement);
var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var string = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
document.write(string);
};
for(var i=0;i<5;++i){
output += '<p><h2><a href="' +
data.items[i].link + '" target="_blank" >' +
data.items[i].title + '</h2></a></p><p>' +
formatDate(data.items[i].pubDate) + '</p>';
}
content.innerHTML = output;
}
}
</script>
<script type="text/javascript" src="http://rss2json.com/api.json?callback=showFeed&rss_url=http%3A%2F%2Fmix.chimpfeedr.com%2F36116-Insurance-News"></script>
另外,有没有办法像这样格式化日期? 2016 年 10 月 10 日14:00
要按照您在问题中建议的那样格式化 Feed 的 pubDate
,请替换此行:
var string = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
有了这个:
var string = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear() + " " + addZero(date.getHours()) + ":" + addZero(date.getMinutes());
我稍微更改了您的代码,结果如下:
$(document).ready(function() {
loadNews();
});
// Load news:
function loadNews() {
$.ajax({
url: "http://rss2json.com/api.json?callback=showFeed&rss_url=http%3A%2F%2Fmix.chimpfeedr.com%2F36116-Insurance-News",
type: "GET",
dataType: 'jsonp',
headers: {
"accept": "application/json;odata=verbose",
},
success: function(data) {
var feedlength = data.items.length;
var finalHTML = "";
var HTML_DIV = document.getElementById('divAllNews');
// Validar que haya encontrado el div.
if (HTML_DIV != null && HTML_DIV != undefined) {
// Validar que la consulta traiga datos.
if (feedlength != null && feedlength > 0) {
for (var i = 0; i < feedlength; i++) {
finalHTML += "<div>" +
" <p>" + "<a href='" + data.items[i].link + "'>" + data.items[i].title + "</a>" + "</p>" +
" <p>" + "Date: " + formatDate(data.items[i].pubDate) + "</p>" +
"</div>";
}
}
// Colocar los datos.
HTML_DIV.innerHTML = (finalHTML != "") ? finalHTML : "No results";
}
},
error: function(err) {
alert('An error occurred.');
}
});
}
// Format date:
function formatDate(myElement) {
var date = new Date(myElement);
var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var string = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear() + " " + addZero(date.getHours()) + ":" + addZero(date.getMinutes());
return string;
}
// Add zero. Source: http://www.w3schools.com/jsref/jsref_gethours.asp
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<div id="divAllNews"><i>Loading news...</i></div>
我无法在每个 RSS 项目标题下显示日期。日期显示,但它们只显示在我页面的底部。我一直在反复试验,但在这一点上我被困了一段时间。
<script type="text/javascript">
var content = document.getElementById('insurance-news');
function showFeed(data){
if(data.status == 'ok'){
var feedlength = data.items.length;
var output = '';
function formatDate(myElement){
var date = new Date(myElement);
var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var string = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
document.write(string);
};
for(var i=0;i<5;++i){
output += '<p><h2><a href="' +
data.items[i].link + '" target="_blank" >' +
data.items[i].title + '</h2></a></p><p>' +
formatDate(data.items[i].pubDate) + '</p>';
}
content.innerHTML = output;
}
}
</script>
<script type="text/javascript" src="http://rss2json.com/api.json?callback=showFeed&rss_url=http%3A%2F%2Fmix.chimpfeedr.com%2F36116-Insurance-News"></script>
另外,有没有办法像这样格式化日期? 2016 年 10 月 10 日14:00
要按照您在问题中建议的那样格式化 Feed 的 pubDate
,请替换此行:
var string = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
有了这个:
var string = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear() + " " + addZero(date.getHours()) + ":" + addZero(date.getMinutes());
我稍微更改了您的代码,结果如下:
$(document).ready(function() {
loadNews();
});
// Load news:
function loadNews() {
$.ajax({
url: "http://rss2json.com/api.json?callback=showFeed&rss_url=http%3A%2F%2Fmix.chimpfeedr.com%2F36116-Insurance-News",
type: "GET",
dataType: 'jsonp',
headers: {
"accept": "application/json;odata=verbose",
},
success: function(data) {
var feedlength = data.items.length;
var finalHTML = "";
var HTML_DIV = document.getElementById('divAllNews');
// Validar que haya encontrado el div.
if (HTML_DIV != null && HTML_DIV != undefined) {
// Validar que la consulta traiga datos.
if (feedlength != null && feedlength > 0) {
for (var i = 0; i < feedlength; i++) {
finalHTML += "<div>" +
" <p>" + "<a href='" + data.items[i].link + "'>" + data.items[i].title + "</a>" + "</p>" +
" <p>" + "Date: " + formatDate(data.items[i].pubDate) + "</p>" +
"</div>";
}
}
// Colocar los datos.
HTML_DIV.innerHTML = (finalHTML != "") ? finalHTML : "No results";
}
},
error: function(err) {
alert('An error occurred.');
}
});
}
// Format date:
function formatDate(myElement) {
var date = new Date(myElement);
var months = Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
var string = months[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear() + " " + addZero(date.getHours()) + ":" + addZero(date.getMinutes());
return string;
}
// Add zero. Source: http://www.w3schools.com/jsref/jsref_gethours.asp
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<div id="divAllNews"><i>Loading news...</i></div>