格式化日期 JS
Date.prototype.format = function (timeFmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(timeFmt)) {
timeFmt= timeFmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(timeFmt)) {
timeFmt= timeFmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return timeFmt;
}
var x = new Date().format("yyyy-MM-dd hh:mm:ss");
console.log(x);
var time2 = new Date().format("yyyy-MM-dd");
console.log(time2);
var oldTime = (new Date("2022/02/24 19:38:41")).getTime();
var curTime = new Date(oldTime).format("yyyy-MM-dd");
console.log(curTime);