js获取当前时间(年-月-日 时:分:秒)

直接拷贝函数即可使用

function nowTime() {//获取当前时间
    let now= new Date();
    let _month = ( 10 > (now.getMonth()+1) ) ? '0' + (now.getMonth()+1) : now.getMonth()+1;
    let _day = ( 10 > now.getDate() ) ? '0' + now.getDate() : now.getDate();
    let _hour = ( 10 > now.getHours() ) ? '0' + now.getHours() : now.getHours();
    let _minute = ( 10 > now.getMinutes() ) ? '0' + now.getMinutes() : now.getMinutes();
    let _second = ( 10 > now.getSeconds() ) ? '0' + now.getSeconds() : now.getSeconds();
    return now.getFullYear() + '-' + _month + '-' + _day + ' ' + _hour + ':' + _minute + ':' + _second;
}

用法:

let time = nowTime();
//2019-12-12 12:12:12
发布了101 篇原创文章 · 获赞 69 · 访问量 22万+

猜你喜欢

转载自blog.csdn.net/weekdawn/article/details/103772443
今日推荐