把JS里面的Date规范输出为“YYYY-MM-DD HH:mm:SS”的字符串

it2022-05-09  25

今天做页面需要把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; }

转载于:https://www.cnblogs.com/nanshouyong326/archive/2008/12/30/1365103.html


最新回复(0)