美文网首页
js获取当前月的开始日期和结束日期

js获取当前月的开始日期和结束日期

作者: Hello杨先生 | 来源:发表于2020-02-02 01:54 被阅读0次
          //获取月份
          getAlldate(item){
            console.log(item)
            var now = new Date(); //当前日期
            var nowDayOfWeek = now.getDay(); //今天本周的第几天
            var nowDay = now.getDate(); //当前日
            // var nowMonth = now.getMonth(); //当前月
            var nowMonth = item-1; //当前月
            var nowYear = now.getYear(); //当前年
            nowYear += (nowYear < 2000) ? 1900 : 0; //
            var lastMonthDate = new Date(); //上月日期
            lastMonthDate.setDate(1);
            lastMonthDate.setMonth(lastMonthDate.getMonth()-1);
            var lastYear = lastMonthDate.getYear();
            var lastMonth = lastMonthDate.getMonth();
            var monthStartDate = new Date(nowYear, nowMonth, 1);
            console.log('start_time',this.formatDate(monthStartDate))
            var days = new Date(nowDay, item, 0).getDate();
            console.log(days)
            var monthEndDate = new Date(nowYear, nowMonth, days);
            console.log('end_time',this.formatDate(monthEndDate))
    
            this.params.start_time = this.formatDate(monthStartDate)
            this.params.end_time = this.formatDate(monthEndDate)
    
            this.getTranRecord()  //调用获取数据方法
    
          },
          // 格式化日期
          formatDate(date) {
            var myyear = date.getFullYear();
            var mymonth = date.getMonth()+1;
            var myweekday = date.getDate();
    
            if(mymonth < 10){
              mymonth = "0" + mymonth;
            }
            if(myweekday < 10){
              myweekday = "0" + myweekday;
            }
            return (myyear+mymonth + myweekday);
          },
    
    
    image.png

    相关文章

      网友评论

          本文标题:js获取当前月的开始日期和结束日期

          本文链接:https://www.haomeiwen.com/subject/mtufxhtx.html