查詢年月日
let dy='日'let listtime= maxDay('2022-12-31',dy)console.log(listtime);function maxDay(lastYear,dy){//查詢?nèi)掌谀暝氯盏奶幚? ? let Timesend,Timestate? ? let lastDate=new Date(lastYear)? ? let lastYear1=lastDate.getFullYear()? ? let lastMonth=lastDate.getMonth()+1? ? let lastgetDate=lastDate.getDate()? ? let lastYearMonth=new Date(lastYear1,lastMonth,0)? ? let lastYearMonthdate=lastYearMonth.getDate()//獲取查詢月的最后一天? ? let thisDate=new Date()? ? let thisYear1=thisDate.getFullYear()? ? let thisMonth=thisDate.getMonth()+1? ? let lastgetDate1=lastgetDate+1>lastYearMonthdate?01:lastgetDate+1? ?let lastMonth1=dy=='日'&&lastgetDate<lastYearMonthdate?by2(lastMonth):lastMonth+1>12?'01':by2(lastDate.getMonth()+2)? ? let lastYear2=lastMonth+1>12&&dy=='月'?lastDate.getFullYear()+1:lastMonth+1>12&&dy=='日'&&lastgetDate+1>lastYearMonthdate?lastDate.getFullYear()+1:dy=='年'?lastDate.getFullYear()+1:lastDate.getFullYear()? ? if(lastYear1==thisYear1 && thisMonth==lastMonth || lastYear1==thisYear1 && dy=='年'){? ? ? ? ?Timestate=lastYear1+'-'+`${dy=='年'?'01':by2(lastMonth)}-${dy=='年'?'01':dy=='月'?'01':by2(lastgetDate)} 08:00:00`? ? ? ? ?Timesend=lastYear1+'-'+`${by2(lastMonth1)}-${by2(lastgetDate1)} 08:00:00`? ? }else{? ? ? ?Timestate=lastYear1+'-'+`${dy=='年'?'01':by2(lastMonth)}-${dy=='年'?'01':dy=='月'?'01':by2(lastgetDate)} 08:00:00`? ? ? ?Timesend=`${lastYear2}`+'-'+`${dy=='年'?'01':dy=='日'?lastMonth1:lastMonth1}-${dy=='年'?'01':dy=='月'?'01':by2(lastgetDate1)} 08:00:00`? ? }? ?return {Timesend,Timestate}}
function by2(params) {? ? return params<10?'0'+params:params}