JavaScript 获取当前日期和三十天以前日期

mac2024-04-04  36

function getThirtyDays() { //获取当前日期 var myDate = new Date(); var nowY = myDate.getFullYear(); var nowM = myDate.getMonth()+1; var nowD = myDate.getDate(); var enddateStr = nowY+"-"+(nowM<10 ? "0" + nowM : nowM)+"-"+(nowD<10 ? "0"+ nowD : nowD);//当前日期 var enddate = new Date(enddateStr); //获取三十天前日期 var lw = new Date(myDate - 1000 * 60 * 60 * 24 * 30);//最后一个数字30可改,30天的意思 var lastY = lw.getFullYear(); var lastM = lw.getMonth()+1; var lastD = lw.getDate(); var startdateStr =lastY+"-"+(lastM<10 ? "0" + lastM : lastM)+"-"+(lastD<10 ? "0"+ lastD : lastD);//三十天之前日期 var startDate = new Date(startdateStr); const dateList = [] while(true) { startDate.setDate(startDate.getDate() + 1) if (startDate.getTime() <= enddate.getTime()) { if (startDate.getDate() < 10) { // startDate.getFullYear() 获取年,此处没加上年份 dateList.push((startDate.getMonth()+1) + '.0' + startDate.getDate()) } else { dateList.push((startDate.getMonth()+1) + '.' + startDate.getDate()) } } else { break } } return dateList; }

 

最新回复(0)