方案三,同方案一,只是加入了日期转时间戳操作:
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); },