js获取当前时间,小于10前边补0
var time = function() {
var date = new Date();
//年
var year = date.getFullYear();
//月
var month = (date.getMonth() + 1) < 10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1); //日 var
day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate(); //时 var
hh = date.getHours() < 10 ? "0" + date.getHours() : date.getHours(); //分 var
mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes(); //秒 var
ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var nowTime = year + "-" + month + "-" + day + " " + hh + ": " + mm + ": " + ss;
return nowTime;
}
console.log("当前时间是 " + time()) //当前时间是2020-07-28 15:58:04
本文标题:js获取当前时间,小于10前边补0
本文链接:https://www.haomeiwen.com/subject/rokmprtx.html
网友评论