获取本周或本月日期

1.有个需求要获得本周和本月日期代码如下, this.$u.timeFormat是自己封装的全局改变时间格式函数

			// 获取本周日期
			getMouthTime(e) {
				// 获取当前日期
				let currentDate = new Date();
				// 获取当前日期的星期几(0-6,其中0代表星期日)
				let currentDay = currentDate.getDay();
				// 计算距离本周第一天的天数差值
				let diff = currentDate.getDate() - currentDay + (currentDay === 0 ? -6 : 1);
				if(e == 'month') {
					// 获取本月第一天的日期
					this.start_date = new Date(currentDate.getFullYear(), currentDate.getMonth(), 1);
					// 获取下个月第一天的日期
					let nextMonthFirstDay = new Date(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
					// 使用下个月第一天的日期减去一天,得到本月最后一天的日期
					this.end_date = new Date(nextMonthFirstDay.getTime() - 1);
				} else {
					// 计算本周第一天的日期
                     this.start_date = new Date(currentDate.setDate(diff));
					// 计算本周最后一天的日期
					this.end_date = new Date(currentDate.setDate(currentDate.getDate() + 6));
				}
				// this.start_date = this.start_date.toLocaleDateString().replace(/\//g,"-")
				this.start_date = this.$u.timeFormat(this.start_date, 'yyyy-mm-dd');
				this.end_date = this.$u.timeFormat(this.end_date, 'yyyy-mm-dd');
			},

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值