时间格式化函数

function getNow() {
    var tempDate = new Date(Date.now());
    var year = tempDate.getFullYear();
    var month = tempDate.getMonth() + 1;
    if(month < 10){
        month = 0 + month.toString();
    }
    var day = tempDate.getDate();
    if(day < 10){
        day = 0 + day.toString();
    }
    var hour = tempDate.getHours();
    if(hour < 10){
        hour = 0 + hour.toString();
    }
    var minute = tempDate.getMinutes();
    if(minute < 10){
        minute = 0 + minute.toString();
    }
    var seconds = tempDate.getSeconds();
    if(seconds < 10){
        seconds = 0 + seconds.toString();
    }
    return `${year}-${month}-${day} ${hour}:${minute}:${seconds}`;
}

module.exports.getNow = getNow;

  

posted @ 2022-03-02 14:25  我的世界开始下雪  阅读(21)  评论(0编辑  收藏  举报