JS获取当前时间

68 阅读1分钟
const getDay = (num) => {
    return num<10?'0'+num:num
}

const currentDate = new Date();
const year = currentDate.getFullYear();
const month = currentDate.getMonth() + 1; // 月份从0开始,所以需要加1
const day = currentDate.getDate();
const hours = currentDate.getHours();
const minutes = currentDate.getMinutes();
const seconds = currentDate.getSeconds();
console.log(`${year}-${getDay(month)}-${getDay(day)} ${getDay(hours)}:${getDay(minutes)}:${getDay(seconds)}`) //2024-06-14 16:17:30