帮助了 AmazingCounters.com 位小伙伴

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

 

 

 

posted on 2021-05-26 10:38  云的旋律  阅读(3659)  评论(0编辑  收藏  举报

导航

前端攻城狮分享群