我有一个使用javascript创建的RSS提要阅读器,需要格式化日期。目前,pubDate设置为.toLocaleDateString,并在各种计算机上显示不同。
有人知道我是否可以将其设置为标准的dd / mm / yyyy格式吗?
以下完整的javascript:
(function ($) {
var current = null;
$.fn.rssfeed = function (url, options) {
var defaults = {
limit: 10,
header: true,
titletag: 'h4',
date: true,
content: true,
snippet: true,
showerror: true,
errormsg: '',
key: null
};
var options = $.extend(defaults, options);
return this.each(function (i, e) {
var $e = $(e);
if (!$e.hasClass('rssFeed')) $e.addClass('rssFeed');
if (url == null) return false;
var api = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=" + url;
if (options.limit != null) api += "&num=" + options.limit;
if (options.key != null) api += "&key=" + options.key;
$.getJSON(api, function (data) {
if (data.responseStatus == 200) {
_callback(e, data.responseData.feed, options);
} else {
if (options.showerror) if (options.errormsg != '') {
var msg = options.errormsg;
} else {
var msg = data.responseDetails;
};
$(e).html('<div class="rssError"><p>' + msg + '</p></div>');
};
});
});
};
var _callback = function (e, feeds, options) {
if (!feeds) {
return false;
}
var html = '';
var row = 'odd';
if (options.header) html += '';
html += '<div class="rssBody">' + '<ul>';
for (var i = 0; i < feeds.entries.length; i++) {
var entry = feeds.entries[i];
var entryDate = new Date(entry.publishedDate);
var pubDate = entryDate.toLocaleDateString() + ' ' + entryDate.toLocaleTimeString();
html += '<li class="rssRow ' + row + '">' + '<' + options.titletag + '><a href="' + entry.link + '" title="View this feed at ' + feeds.title + '" class="blog">' + entry.title + '</a></' + options.titletag + '>'
if (options.date) html += '<p class="date">' + pubDate + '</p>'
if (options.content) {
if (options.snippet && entry.contentSnippet != '') {
var content = entry.contentSnippet;
} else {
var content = entry.content;
}
html += '<p>' + content + '</p><p><a href="' + entry.link + '" class="blogbtn">continue reading</a></p>'
}
html += '</li>';
if (row == 'odd') {
row = 'even';
} else {
row = 'odd';
}
}
html += '</ul>' + '</div>'
$(e).html(html);
};
})(jQuery);
答案 0 :(得分:0)
你可以做到
var pubDate = entryDate.format("dd/mm/yyyy");
答案 1 :(得分:0)
您的格式的具体实施: 这里:
Date.prototype.ddmmyy = function(){
function p(n) {return (n>9)?n:"0"+n}
var m = this.getMonth()
, d = this.getDate()
, y = this.getFullYear();
return [p(d),p(m+1),y%100].join("/")
}
将您的特定请求合并到您提供的代码中:
(function($){var current=null;$.fn.rssfeed=function(url,options){var defaults={limit:10,header:true,titletag:'h4',date:true,content:true,snippet:true,showerror:true,errormsg:'',key:null};var options=$.extend(defaults,options);return this.each(function(i,e){var $e=$(e);if(!$e.hasClass('rssFeed'))$e.addClass('rssFeed');if(url==null)return false;var api="http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+url;if(options.limit!=null)api+="&num="+options.limit;if(options.key!=null)api+="&key="+options.key;$.getJSON(api,function(data){if(data.responseStatus==200){_callback(e,data.responseData.feed,options);}else{if(options.showerror)
if(options.errormsg!=''){var msg=options.errormsg;}else{var msg=data.responseDetails;};$(e).html('<div class="rssError"><p>'+msg+'</p></div>');};});});};var _callback=function(e,feeds,options){if(!feeds){return false;}
var html='';var row='odd';if(options.header)
html+='';html+='<div class="rssBody">'+'<ul>';for(var i=0;i<feeds.entries.length;i++){var entry=feeds.entries[i];var entryDate=new Date(entry.publishedDate);var pubDate=entryDate.ddmmyy()+' '+entryDate.ddmmyy();html+='<li class="rssRow '+row+'">'+'<'+options.titletag+'><a href="'+entry.link+'" title="View this feed at '+feeds.title+'" class="blog">'+entry.title+'</a></'+options.titletag+'>'
if(options.date)html+='<p class="date">'+pubDate+'</p>'
if(options.content){if(options.snippet&&entry.contentSnippet!=''){var content=entry.contentSnippet;}else{var content=entry.content;}
html+='<p>'+content+'</p><p><a href="'+entry.link+'" class="blogbtn">continue reading</a></p>'}
html+='</li>';if(row=='odd'){row='even';}else{row='odd';}}
html+='</ul>'+'</div>'
$(e).html(html);};})(jQuery);
但是,更通用的实现 - 支持作为参数提供的格式:
function(f) {
if (!this.isValid()) return 'invalid date';
f = f || '%x %X';
f = formats[f.toLowerCase()] || f; // replace short-hand with actual format
var d = this;
return f.replace(/%([a-z%])/gi, function($0, $1) {
switch ($1) {
case 'a':
return Date.getMsg('days')[d.get('day')].substr(0, 3);
case 'A':
return Date.getMsg('days')[d.get('day')];
case 'b':
return Date.getMsg('months')[d.get('month')].substr(0, 3);
case 'B':
return Date.getMsg('months')[d.get('month')];
case 'c':
return d.toString();
case 'd':
return pad(d.get('date'), 2);
case 'D':
return d.get('date');
case 'e':
return d.get('date');
case 'H':
return pad(d.get('hr'), 2);
case 'I':
return ((d.get('hr') % 12) || 12);
case 'j':
return pad(d.get('dayofyear'), 3);
case 'm':
return pad((d.get('mo') + 1), 2);
case 'M':
return pad(d.get('min'), 2);
case 'o':
return d.get('ordinal');
case 'p':
return Date.getMsg(d.get('ampm'));
case 's':
return Math.round(d / 1000);
case 'S':
return pad(d.get('seconds'), 2);
case 'U':
return pad(d.get('week'), 2);
case 'w':
return d.get('day');
case 'x':
return d.format(Date.getMsg('shortDate'));
case 'X':
return d.format(Date.getMsg('shortTime'));
case 'y':
return d.get('year').toString().substr(2);
case 'Y':
return d.get('year');
case 'T':
return d.get('GMTOffset');
case 'Z':
return d.get('Timezone');
case 'z':
return pad(d.get('ms'), 3);
}
return $1;
});
}
答案 2 :(得分:0)
您可以通过实施preprocess
回调来操纵每个条目的属性。假设您要修改条目&#39; publishedDate
格式:
$('#container').feeds({
feeds: {
// Your feeds ...
},
preprocess: function ( feed ) {
// Change the publishedDate format from UTC to dd-mm-yyyy
// Inside the callback 'this' corresponds to the entry being processed
var date = new Date(this.publishedDate);
var months = date.getMonth() + 1;
if ( months <= 9) months = '0' + months; //adding leading zeros
var days = date.getDate();
if (days <= 9) days = '0' + days; //adding leading zeros
var pieces = [days, months, date.getFullYear()]
this.publishedDate = pieces.join('/');
}
});
您还可以使用moment.js
等js库希望有所帮助。