我在一个看起来像“2013 年 4 月 30 日”的 div 中有日期,我想将其转换为:4 月 30 日星期二 | 2013
我已经编写了一些代码来为我进行这种转换。它在 Chrome 中工作正常,但在 Firefox 和 firebug 控制台中无法正常工作,它说: Date {Invalid Date} 并显示输出看起来像 NaN undefined undefined | 南。我的代码如下所示,或者您也可以看到这个 Fiddle:
(function ( $ ) {
$.fn.bcDateModify = function() {
return this.each(function() {
var obj = this;
var srcDate= $(obj).html();
srcDate = srcDate.replace(/\s+/g, '');
objDate = new Date(srcDate);
console.log(objDate);
var newDate = objDate.getDate();
var newDay = objDate.getDay();
var newMonth = objDate.getMonth();
var newYear = objDate.getFullYear();
var weekday=new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var monthNames=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var myhtml ='<div class="date"><span>'+newDate+'</span></div><div class="month-day"><h3>'+weekday[newDay]+'</h3><span>'+monthNames[newMonth]+' | '+newYear+'</span></div>';
$(obj).html(myhtml);
});
};
}( jQuery ));
$(document).ready(function(){
$('.date-obj').bcDateModify();
});