00:00:00
js获取此刻时间
js 获取当前时间
/**
* @param {Number} stamp [为空或者填入需要转化的时间戳]
*/
const currentTime = (stamp) => {
const date = stamp ? new Date(stamp) : new Date();
let year = timeFormat(date.getFullYear());
let month = timeFormat(date.getMonth() + 1);
let day = timeFormat(date.getDate());
let hour = timeFormat(date.getHours());
let min = timeFormat(date.getMinutes());
let sec = timeFormat(date.getSeconds());
return `${year}-${month}-${day} ${hour}:${min}:${sec}`;
}
const timeFormat = (data)=>{//格式化时间
data = '0' + data;
data = data.slice(-2);//从data中提取倒数第二个到最后一个(参数为数组下标)
return data;
}
console.log(currentTime());//(举例)21-12-08 10:00:26可根据需要的格式自行拼凑,或者把格式做成参数,这里只是提供一个写法思路