* 获取近半年日期 function halfYear() { // 先获取当前时间 let curDate = (new Date()).getTime(); // 将半年的时间单位换算成毫秒 let halfYear = 365 / 2 * 24 * 3600 * 1000; // 半年前的时间(毫秒单位) let pastResult = curDate - halfYear; // 日期函数,定义起点为半年前 let pastDate = new Date(pastResult); let pastYear = pastDate.getFullYear(); let pastMonth = pastDate.getMonth() + 1; pastMonth = pastMonth.toString().padStart(2, 0); let pastDay = pastDate.getDate().toString().padStart(2, 0); let endDate = pastYear + '-' + pastMonth + '-' + pastDay; return endDate; }
1 2 3 4 5 6 7 8 9 10 11 12
* 获取近一年日期 function reactYear() { let nowDate = new Date(); let dates = new Date(nowDate); dates.setDate(dates.getDate() - 365); let year = dates.getFullYear(); let month = dates.getMonth() + 1; month = month.toString().padStart(2, 0); let strDate = dates.getDate().toString().padStart(2, 0); let currentDate = year + '-' + month + '-' + strDate; return currentDate; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
* 获取当前日期 function getCurDate(type) { const date = new Date(); // date.setTime(date.getTime() + 24 * 60 * 60 * 1000); date.setTime(date.getTime()); let year = date.getFullYear() let month = date.getMonth() + 1 month = month.toString().padStart(2, 0); let strDate = date.getDate().toString().padStart(2, 0); let hour = date.getHours().toString().padStart(2, 0); let minutes = date.getMinutes().toString().padStart(2, 0); let seconds = date.getSeconds().toString().padStart(2, 0); let curDate = year + '-' + month + '-' + strDate; if(type) { return `${year}-${month}-${strDate} ${hour}:${minutes}:${seconds}`; } return curDate; }