获取当前日期并格式化输出
let timeFormat = function() {
//获取当前时间
let date = new Date()
// 获取当前年份
let year = date.getFullYear()
//获取当前月份
let month = date.getMonth() + 1
//补0
month = month < 0 ? '0' + month : month
//获取当前日
let day = date.getDate()
//补0
day = day < 0 ? '0' + day : day
//获取当前小时
let hour = date.getHours()
//补0
hour = hour < 0 ? '0' + hour : hour
//获取当前分钟
let minute = date.getMinutes()
//补0
minute = minute < 0 ? '0' + minute : minute
//获取当前秒数
let second = date.getSeconds()
//补0
second = second < 0 ? '0' + second : second
//返回日期
return `${year}-${month}-${day} ${hour}:${minute}:${second}`
}
timeFormat()
\