获取前一天的当前时刻和当前时刻
mounted() {
this.searchForm.endTime = this.getNow()
this.searchForm.startTime = new Date(new Date().setDate(new Date().getDate() - 1)) // 获取当前日期前一天的Date对象
this.searchForm.startTime = this.dataFormat(this.searchForm.startTime)
},
methods: {
// 格式化时间
dataFormat(date) {
// date是传入的时间
let d = new Date(date)
let month = d.getMonth() + 1 < 10 ? '0' + (d.getMonth() + 1) : d.getMonth() + 1
let day = d.getDate() < 10 ? '0' + d.getDate() : d.getDate()
let hours = d.getHours() < 10 ? '0' + d.getHours() : d.getHours()
let min = d.getMinutes() < 10 ? '0' + d.getMinutes() : d.getMinutes()
let sec = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds()
let times =
d.getFullYear() +
'-' +
month +
'-' +
day +
' ' +
hours +
':' +
min +
':' +
sec
return times
},
getNow() {
var date = new Date()
let y = date.getFullYear()
let MM = date.getMonth() + 1
MM = MM < 10 ? ('0' + MM) : MM
let d = date.getDate()
d = d < 10 ? ('0' + d) : d
let h = date.getHours()
h = h < 10 ? ('0' + h) : h
let m = date.getMinutes()
m = m < 10 ? ('0' + m) : m
let s = date.getSeconds()
s = s < 10 ? ('0' + s) : s
var time = y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
return time // 时间 2023-04-19 13:10:20 想要字符串可以转一下toString
},
}