// 获取当前时间
getDate() {
const myDate = new Date()
//获取当前年
const year = myDate.getFullYear()
//获取当前月
const month = myDate.getMonth() + 1
//获取当前日
const date = myDate.getDate()
//获取当前小时数(0-23)
const h = myDate.getHours()
//获取当前分钟数(0-59)
const m = myDate.getMinutes()
const s = myDate.getSeconds()
//获取当前时间
const time =
year +
'-' +
this.convert(month) +
'-' +
this.convert(date) +
' ' +
this.convert(h) +
':' +
this.convert(m) +
':' +
this.convert(s)
return time
},
//日期时间处理
convert(val) {
return val < 10 ? '0' + val : val
}
这样,可以获取到当前日期和时间,并且已经转换成了标准格式,结果如下: