忘了在哪儿看的一段代码了。保存下来
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
var month = date.getMonth() + 1;
var strDate = date.getDate();
var minutes = date.getMinutes();
var getSeconds = date.getSeconds();
if (month >= 0 && month <= 9) {
month = "0" + month;
}
if (minutes >= 0 && minutes <= 9) {
minutes = "0" + minutes;
}
if (getSeconds >= 0 && getSeconds <= 9) {
getSeconds = "0" + getSeconds;
}
if (strDate >= 0 && strDate <= 9) {
strDate = "0" + strDate;
}
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + minutes+ seperator2 + date.getSeconds();
console.log(currentdate)
结果:2018-06-16 15:00:57