javascript 获取当前系统时间
// 获取当前时间函数
function getCurrentTimer (format1, format2) {
const time = new Date()
const year = time.getFullYear()
const obj = {
month: time.getMonth(),
date: time.getDate(),
hour: time.getHours(),
minutes: time.getMinutes(),
seconds: time.getSeconds()
}
for (let key in obj) {
if (key === 'month') {
obj[key] = obj[key] + 1
}
if (obj[key] < 10) {
obj[key] = '0' + obj[key]
}
}
return year + format1 + obj.month + format1 + obj.date + ' ' + obj.hour + format2 + obj.minutes + format2 + obj.seconds
}
// 调用格式
getCurrentTimer('-', ':') // 设置时间显示格式 2020-11-24 12:24:30
getCurrentTimer('/', ':') // 设置时间显示格式 2020/11/24 12:24:30