最后
开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】
dateObj.start = year + '-' + (month - 1) + '-' + startMonthDay
}
} else {
dateObj.start = year + '-' + (month - 1) + '-' + day
}
}
const newMonthDate = [dateObj.start + ’ ’ + ‘00:00:00’, dateObj.end + ’ ’ + ‘00:00:00’]
return newMonthDate
}
// 获取近三个月日期范围
export function getLastThreeMonthDate() {
if (month - 3 <= 0) { // 如果是1、2、3月,年数往前推一年
const start3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate() // 3个月前所在月的总天数
if (start3MonthDay < day) { // 3个月前所在月的总天数小于现在的天日期
dateObj.start = (year - 1) + ‘-’ + (12 - (3 - month)) + ‘-’ + start3MonthDay
} else {
dateObj.start = (year - 1) + ‘-’ + (12 - (3 - month)) + ‘-’ + day
}
} else {
const start3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate() // 3个月前所在月的总天数
if (start3MonthDay < day) { // 3个月前所在月的总天数小于现在的天日期
if (day < endMonthDay) { // 当前天日期小于当前月总天数,2月份比较特殊的月份
dateObj.start = year + ‘-’ + (month - 3) + ‘-’ + (start3MonthDay - (endMonthDay - day))
} else {
dateObj.start = year + ‘-’ + (month - 3) + ‘-’ + start3MonthDay
}
} else {
dateObj.start = year + ‘-’ + (month - 3) + ‘-’ + day
}
}
const newThreeMonthDate = [dateObj.start + ’ ’ + ‘00:00:00’, dateObj.end + ’ ’ + ‘00:00:00’]
return newThreeMonthDate
}
**二、vue页面使用的时候引入:**
import { getLastWeekDate, getLastMonthDate, getLastThreeMonthDate } from ‘@/utils/latelyTime’
console.log('最近一周:', getLastWeekDate(null))
console.log('最近一个月:', getLastMonthDate(null))
console.log('最近三个月:', getLastThreeMonthDate (null))
**三、控制台打印结果:**
![](https://img-blog.csdnimg.cn/b1c275fecde340069d50328f9712f829.png)
**四、注意:**我这里把开始时间和结束时间都 + 1天了,因为我们的后台要求的,不加的话他查不到今天的数据,(哈哈哈,其实显示这里加了不太友好, 应该是后端查询的时候自动把开始、结束时间+1天去查,才更友好。)
不想**+1天**的话这样处理:
**把明天的时间注释掉,解开今天的时间,就OK了。**![](https://img-blog.csdnimg.cn/937395b1ef4e4566b330901bfa3db991.png)
**五、打印结果如下:**
![](https://img-blog.csdnimg.cn/09d56893cc7d42c183157363ad50d52a.png)
**六、**有的同学说我不想要时分秒怎么处理,来来看看组件的return 的地方很简单:
所以return的这里把我圈住的部分就去除掉,不要了。这里我就不做演示打印了。
![](https://img-blog.csdnimg.cn/e12073995dc8472b9e3c6f2785afa34f.png)
### 文末
篇幅有限没有列举更多的前端面试题,小编把整理的前端大厂面试题PDF分享出来,一共有269页
![](https://img-blog.csdnimg.cn/img_convert/04273c2f50e58b6822663c5b2f379206.png)
![](https://img-blog.csdnimg.cn/img_convert/081b5236ebd9ff24af676bcd4a51c99e.png)
**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)**
【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0)**