本文主要是介绍JS根据当前时间计算出过去七天的日期,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
项目场景:JS根据当前时间计算出过去七天的日期
提示:近七天场景
问题描述:
提示:正向就是未来,负向就是过去可操作空间还是可以的(年份被我注释掉了):
例如:
let date = new Date();this.getLastOrNextFewDateBy(date)// 获取过去七天日期包含今天getLastOrNextFewDateBy (value) {this.timeList = []let timedate = value !== '' ? value : this.bigtime !== '' ? this.bigtime : '';for (let i = -6; i <= 0; i++) {let today = new Date();if (timedate) {this.receiver = timedate;} else {this.receiver = today;}let lastOrNextDate = this.receiver.getTime() + 1000 * 60 * 60 * 24 * i;today.setTime(lastOrNextDate);// var year = today.getFullYear();let month = today.getMonth() + 1;if (this.receiver == timedate) {let date = today.getDate();if (date < 10) {date = '0' + date}if (month < 10) {month = '0' + month}this.timeList.push(month + "月" + date + '日')} else {let date = today.getDate() - 1;if (date < 10) {date = '0' + date}if (month < 10) {month = '0' + month}this.timeList.push(month + "月" + date + '日')}// this.timeList.push(i != 0 ? month + "月" + date + '日' : '本日')// this.timeList.push(month + "月" + date + '日')}console.log(this.timeList)},
结果:
提示:时间计算笔记001号
## 方法2
// 获取过去七天包含今天的日期(不包含年月)get7day() {var arr = [];var now = new Date().getTime();var oneDayTime = 60 * 60 * 24 * 1000;// 一天的秒数for (var i = 6; i >= 0; i--) {var dd = new Date(now - i * oneDayTime).getDate();arr.push(dd < 10 ? '0' + dd : dd + '');}console.log(arr)},
结果
这篇关于JS根据当前时间计算出过去七天的日期的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!