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

文章介绍了在Vue项目中使用的JavaScript函数,用于获取近一周、一个月和三个月的日期范围。同时提及了一位开发者的ACM-ICPC竞赛刷题经历及其编写的算法学习资源,提供了丰富的算法题解和数据结构学习资料。
摘要由CSDN通过智能技术生成

var dateObj = {}
dateObj.start = null
dateObj.end = year + ‘-’ + month + ‘-’ + day
var endMonthDay = new Date(year, month, 0).getDate() // 当前月的总天数

// 获取近一周日期范围
export function getLastWeekDate() {
if (day - 7 <= 0) { // 如果在当月7日之前
const startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate() // 1周前所在月的总天数
if (month - 1 <= 0) { // 如果在当年的1月份
dateObj.start = (year - 1) + ‘-’ + 12 + ‘-’ + (31 - (7 - day))
} else {
dateObj.start = year + ‘-’ + (month - 1) + ‘-’ + (startMonthDay - (7 - day))
}
} else {
dateObj.start = year + ‘-’ + month + ‘-’ + (day - 7)
}
const lastWeekDate = [dateObj.start + ’ ’ + ‘00:00:00’, dateObj.end + ’ ’ + ‘00:00:00’]
return lastWeekDate
}

// 获取近一个月日期范围
export function getLastMonthDate() {
if (month - 1 <= 0) { // 如果是1月,年数往前推一年

dateObj.start = (year - 1) + ‘-’ + 12 + ‘-’ + day
} else {
const startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate()
if (startMonthDay < day) { // 1个月前所在月的总天数小于现在的天日期
if (day < endMonthDay) { // 当前天日期小于当前月总天数
dateObj.start = year + ‘-’ + (month - 1) + ‘-’ + (startMonthDay - (endMonthDay - day))
} else {
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))



### 最后

**小编的一位同事在校期间连续三年参加ACM-ICPC竞赛。从参赛开始,原计划每天刷一道算法题,实际上每天有时候不止一题,一年最终完成了 600+:**

**凭借三年刷题经验,他在校招中很快拿到了各大公司的offer。**



**入职前,他把他的刷题经验总结成1121页PDF书籍,作为礼物赠送给他的学弟学妹,希望同学们都能在最短时间内掌握校招常见的算法及解题思路。**

![](https://img-blog.csdnimg.cn/img_convert/01358b165616eb9429e16241cf00d213.png)

**整本书,我仔细看了一遍,作者非常细心地将常见核心算法题和汇总题拆分为4个章节。**

![](https://img-blog.csdnimg.cn/img_convert/3425ac600577268ce7d1f69b2002194d.png)

**[开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】](https://bbs.csdn.net/topics/618166371)**

**而对于有时间的同学,作者还给出了他结合众多数据结构算法书籍,挑选出的一千多道题的解题思路和方法,以供有需要的同学慢慢研究。**

![](https://img-blog.csdnimg.cn/img_convert/c941214256693bd4eb05782f3f676721.png)
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值