js获取当前时间和前一天的时间
示例代码:
// 获取当前时间和前一天的时间
function getCurrentTimeAndTheDayBeforeTime () {
// 当前时间
const currentTime = new Date()
console.log("currentTime:", currentTime)
// currentTime: Sat Aug 05 2023 09:15:00 GMT+0800 (中国标准时间)
let y = currentTime.getFullYear()
let m = currentTime.getMonth() + 1
let d = currentTime.getDate()
if (d - 1 < 1) {
if (m - 1 < 1) {
y = y - 1
m = 12
} else {
m = m - 1
}
d = new Date(y, m, 0).getDate()
} else {
d = d - 1
}
// 前一天的时间
const theDayBeforeTime = new Date(y, m - 1, d)
console.log("theDayBeforeTime:", theDayBeforeTime)
// theDayBeforeTime: Fri Aug 04 2023 00:00:00 GMT+0800 (中国标准时间)
}
this.getCurrentTimeAndTheDayBeforeTime()
控制台输出如下: