写在Vue项目utils文件下
/**
* 获取指定天数之前日期(返回一个时间区间)
* @param {*} obj
*/
export function getTimeInterval(time) { //time为减去的指定天数
const date1 = new Date()
const date2 = new Date(date1)
// -30为30天前,+30可以获得30天后的日期
date2.setDate(date1.getDate() - time)
// 30天前(日,月份判断是否小于10,小于10的前面+0)
const agoDay = `${date2.getFullYear()}${date2.getMonth() + 1 < 10 ? `0${date2.getMonth() + 1}` : date2.getMonth() + 1}${date2.getDate() < 10 ? `0${date2.getDate()}` : date2.getDate()}`
// 当前日期
const nowDay = `${date1.getFullYear()}${date1.getMonth() + 1 < 10 ? `0${date1.getMonth() + 1}` : date1.getMonth() + 1}${date1.getDate() < 10 ? `0${date1.getDate()}` : date1.getDate()}`
// console.log(`30天前:${agoDay}`)
// console.log(`当前日期:${nowDay}`)
const obj = { startTime: agoDay, endTime: nowDay }
return obj
}
本文介绍了一个Vue项目的实用工具函数,该函数能够生成指定天数之前的日期范围。通过输入一个数值参数,函数可以方便地计算出从当前日期向前推若干天的具体日期,并将其格式化为统一的字符串形式。
&spm=1001.2101.3001.5002&articleId=109530884&d=1&t=3&u=b9dae56f0b0d4d73910ef75cef82e42a)
1675

被折叠的 条评论
为什么被折叠?



