vue js获取最近1周、最近1个月、最近3个月的日期范围_获取近一月的时间

最后

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

    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)**

  • 21
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值