天天看點

js中擷取目前系統的時間,格式2020-10-30 14:05:38

function getCurrentTime() {
    let nowTime = new Date(),
        year = nowTime.getFullYear(),
        month = nowTime.getMonth() + 1 >= 10 ? nowTime.getMonth() + 1 : '0' + (nowTime.getMonth() + 1),
        day = nowTime.getDate() >= 10 ? nowTime.getDate() : '0' + nowTime.getDate(),
        hours = nowTime.getHours() >= 10 ? nowTime.getHours() : '0' + nowTime.getHours(),
        minute = nowTime.getMinutes() >= 10 ? nowTime.getMinutes() : '0' + nowTime.getMinutes(),
        second = nowTime.getSeconds() >= 10 ? nowTime.getSeconds() : '0' + nowTime.getSeconds(),
        currentTime = "";
    currentTime = year + '-' + month + '-' + day + " " + hours + ":" + minute + ":" + second;
    return currentTime;
}  
getCurrentTime(); //"2020-10-30 14:05:38"      
function getCurrentTime() {
    let nowTime = new Date(),
        year = nowTime.getFullYear(),
        month = nowTime.getMonth() + 1 >= 10 ? nowTime.getMonth() + 1 : '0' + (nowTime.getMonth() + 1),
        day = nowTime.getDate() >= 10 ? nowTime.getDate() : '0' + nowTime.getDate(),
        hours = nowTime.getHours() >= 10 ? nowTime.getHours() : '0' + nowTime.getHours(),
        minute = nowTime.getMinutes() >= 10 ? nowTime.getMinutes() : '0' + nowTime.getMinutes(),
        second = nowTime.getSeconds() >= 10 ? nowTime.getSeconds() : '0' + nowTime.getSeconds(),
        currentTime = "";
    currentTime = year + '-' + month + '-' + day + " " + hours + ":" + minute + ":" + second;
    console.log(currentTime);
}
  
var time = setInterval(function(){
    getCurrentTime();
},1000);      
p