js 获取当前月份月头至月尾时间 和当前日期前30天

var oneDayLong = 24 * 60 * 60 * 1000; //每天的总毫秒书
                var now = new Date(); //当前时间
                var sign1 = "-";
                var sign2 = ":";
                var year = now.getFullYear(); //当前年份
                var monthStartDate = new Date(year, now.getMonth() + 1, 1); //当前月1号
                var nextMonthStartDate = new Date(year, now.getMonth() + 2, 1); //下个月1号
                var days = (nextMonthStartDate.getTime() - monthStartDate.getTime()) / oneDayLong; //计算当前月份的天数
                var monthEndDate = new Date(year, now.getMonth() + 1, days);
                var monthRange = [monthStartDate, monthEndDate];
                // 当前月份结束时间
              var currentDateEnd = monthRange[1].getFullYear() + '-' + (monthRange[1].getMonth()<10 ? "0"+ monthRange[1].getMonth() : monthRange[1].getMonth()) + '-' + (monthRange[1].getDate()<10 ? "0"+ monthRange[1].getDate() : monthRange[1].getDate())+ ' ' + (monthRange[1].getHours()<10 ? "0"+ monthRange[1].getHours() : monthRange[1].getHours()) + ':' + (monthRange[1].getMinutes()<10 ? "0"+ monthRange[1].getMinutes() :monthRange[1].getMinutes())+ ':' + (monthRange[1].getSeconds()<10 ? "0"+ monthRange[1].getSeconds() :monthRange[1].getSeconds());
                // 当前月份开始时间
               var  currentDateStart =  monthRange[0].getFullYear() + '-' + (monthRange[0].getMonth()<10 ? "0"+ monthRange[0].getMonth() : monthRange[0].getMonth()) + '-' + (monthRange[0].getDate()<10 ? "0"+ monthRange[0].getDate() : monthRange[0].getDate())+ ' ' + (monthRange[0].getHours()<10 ? "0"+ monthRange[0].getHours() : monthRange[0].getHours()) + ':' + (monthRange[0].getMinutes()<10 ? "0"+ monthRange[0].getMinutes() :monthRange[0].getMinutes())+ ':' + (monthRange[0].getSeconds()<10 ? "0"+ monthRange[0].getSeconds() :monthRange[0].getSeconds());
                
                var lw = new Date(now - 1000 * 60 * 60 * 24 * 30);//最后一个数字30可改,30天的意思
                var lastM = lw.getMonth()+1;//30天前的月份
                var lastD = lw.getDate();//30天前的日期
                 //获取三十天前日期
                var  startdate = year+"-"+(lastM<10 ? "0" + lastM : lastM)+"-"+(lastD<10 ? "0"+ lastD : lastD)+' '+((monthRange[0].getHours()<10 ? "0"+ monthRange[0].getHours() : monthRange[0].getHours()) + ':' + (monthRange[0].getMinutes()<10 ? "0"+ monthRange[0].getMinutes() : monthRange[0].getMinutes()) + ':' + (monthRange[0].getSeconds()<10 ? "0"+ monthRange[0].getSeconds() : monthRange[0].getSeconds()));//三十天之前日期 时间
                //获取当前月份数end
                //获取当前日期begin
                var nowM = now.getMonth()+1;
                var nowD = now.getDate();
                var hour = now.getHours(); // 时
                var minutes = now.getMinutes(); // 分
                var seconds = now.getSeconds() //秒
                var currentdate = year + sign1 + (nowM<10 ? "0" + nowM : nowM)+ sign1 + (nowD<10 ? "0"+ nowD : nowD) + " " + (hour<10 ? "0"+ hour : hour) + sign2 +(minutes<10 ? "0"+ minutes : minutes) + sign2 + (seconds<10 ? "0"+ seconds : seconds)
                var enddate = now+":"+(nowM<10 ? "0" + nowM : nowM)+":"+(nowD<10 ? "0"+ nowD : nowD) ;//当前日期
    原文作者:聰籽
    原文地址: https://blog.csdn.net/weixin_44446223/article/details/89206663
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞