js 獲取本月月初和月末時間操作


 

 

方案三,同方案一,只是加入了日期轉時間戳操作:

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);
 },

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM