本文主要是介绍前端近7天,近半个月,近1个月,近1年的日期处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前端如何获取近7天,近1年的日期进行查询?
methods:{//近7天getRangeDate(ranges) {let nowDays = new Date();let getYear = nowDays.getFullYear();let getMonth = nowDays.getMonth() + 1;let getDate = nowDays.getDate();let nd = new Date();nd = nd.valueOf();nd = nd - ranges * 24 * 60 * 60 * 1000;nd = new Date(nd);let lastYear = nd.getFullYear();let lastMonth = nd.getMonth() + 1;let lastDate = nd.getDate();if (getMonth === 0) {getMonth = '12';getYear = new Date().getFullYear() - 1;} else {getYear = new Date().getFullYear();}if (getMonth < 10) {getMonth = '0' + getMonth;}if (getDate < 10) {getDate = '0' + getDate;}if (lastMonth < 10) {lastMonth = '0' + lastMonth;}if (lastDate < 10) {lastDate = '0' + lastDate;}let nowTime = getYear + '' + getMonth + '' + getDate;console.log('当前日期', getYear, getMonth, getDate);//2024 04 19let lastTime = '';lastTime = lastYear + '' + lastMonth + '' + lastDate;console.log('近7天', lastTime);//20240412let doubleTime = {nowTime: nowTime,lastTime: lastTime,};return doubleTime;},//近1年
isOneYear() {let year = new Date().getFullYear();let lastYear = new Date().getFullYear() - 1;let month = new Date().getMonth() + 1;let getDate = new Date().getDate();if (month < 10) {month = '0' + month;}if (getDate < 10) {getDate = '0' + getDate;}const nowDate =year.toString() + '/' + month.toString() + '/' + getDate.toString();let lastDate =lastYear.toString() + '/' + month.toString() + '/' + getDate.toString();console.log('isOneYear', nowDate, lastDate);//2024/04/19 2023/04/19let nowTime = new Date(nowDate).getTime();let lastTime = new Date(lastDate).getTime();let doubleTime = {nowTime: nowTime,lastTime: lastTime,};return doubleTime;},
}
created(){this.getRangeDate(7);this.isOneYear();
}
这篇关于前端近7天,近半个月,近1个月,近1年的日期处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!