方案三,同方案一,只是加入了日期轉時間戳操作:
getCurrentMonth(){ let firstDate = new Date(); let startDate = firstDate.getFullYear()+"-"+((firstDate.getMonth()+1)<10?"0":"")+(firstDate.getMonth()+1)+"-"+"01"; //alert(firstDate.getFullYear()+"-"+((firstDate.getMonth()+1)<10?"0":"")+(firstDate.getMonth()+1)+"-"+"01"); let date=new Date(); let currentMonth=date.getMonth(); let nextMonth=++currentMonth; let nextMonthFirstDay=new Date(date.getFullYear(),nextMonth,1); let oneDay=1000*60*60*24; let lastDate = new Date(nextMonthFirstDay-oneDay); let endDate = lastDate.getFullYear()+"-"+((lastDate.getMonth()+1)<10?"0":"")+(lastDate.getMonth()+1)+"-"+(lastDate.getDate()<10?"0":"")+lastDate.getDate(); //console.log('開始時間',startDate) this.start_time = Date.parse(startDate) console.log('開始時間====時間戳',Date.parse(startDate)) this.end_time = Date.parse(endDate) //console.log('結束時間', endDate) console.log('結束時間====時間戳', Date.parse(endDate)) //alert(lastDate.getFullYear()+"-"+((lastDate.getMonth()+1)<10?"0":"")+(lastDate.getMonth()+1)+"-"+(lastDate.getDate()<10?"0":"")+lastDate.getDate()); },
方案二:
無論是上一個月還是下一個,或者是指定月份稍加改動編可以
以下例子為計算下個月第一天和最后一天
setDate(){ let curDate = new Date(); let y = curDate.getFullYear(); let m = curDate.getMonth() + 2;//本身就得+1才等於當前月份,然而我要計算下一個月,所以直接+2 if( m > 12 ){ m = 1; y++ } let monthLastDay = new Date(y,m,0).getDate(); this.formxg.syqxks = y + '-' + (m < 10 ? '0'+m : m) + '-' + '01'; this.formxg.syqxjs = y + '-' + (m < 10 ? '0'+m : m) + '-' + (monthLastDay < 10 ? '0'+monthLastDay : monthLastDay); },