今天做页面需要把JS里面的Date规范输出为“YYYY-MM-DD HH:mm:SS”的字符串,
把下面的代码复制到你的JS里,Date对象就可以直接调用toCommonCase():
Date.prototype.toCommonCase=function(){
var xYear=this.getYear();
xYear=xYear+1900;
var xMonth=this.getMonth()+1;
if(xMonth<10){
xMonth="0"+xMonth;
}
var xDay=this.getDate();
if(xDay<10){
xDay="0"+xDay;
}
var xHours=this.getHours();
if(xHours<10){
xHours="0"+xHours;
}
var xMinutes=this.getMinutes();
if(xMinutes<10){
xMinutes="0"+xMinutes;
}
var xSeconds=this.getSeconds();
if(xSeconds<10){
xSeconds="0"+xSeconds;
}
return xYear+"-"+xMonth+"-"+xDay+" "+xHours+":"+xMinutes+":"+xSeconds;
}