直接拷贝函数即可使用
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
获取当前时间的JavaScript函数
本文介绍了一个实用的JavaScript函数,用于获取并格式化当前时间。该函数能够返回形如'2019-12-12 12:12:12'的时间字符串,适用于多种场景。
1388

被折叠的 条评论
为什么被折叠?



