获取近一年,近七日,本月的时间

近一年

    var time = new Date(); //获取当前时间
    time.setTime(time.getTime() + 24 * 60 * 60 * 1000);
    var tomorrow = time.getDate();
    if (parseInt(tomorrow) < 10) {
      tomorrow = "0" + tomorrow;
    }
    var dataArr = [];
    var data = new Date();
    var day = data.getDate(); //日
    var year = data.getFullYear(); //年
    data.setMonth(data.getMonth() + 1, 1); //获取到当前月份,设置月份
    if (parseInt(day) < 10) {
      day = "0" + day;
    }
    for (var i = 0; i < 13; i++) {
      data.setMonth(data.getMonth() - 1); //每次循环一次 月份值减1
      var M = data.getMonth() + 1;
      M = M < 10 ? "0" + M : M;
      dataArr.push(data.getFullYear() + "-" + M);
    }
    var numbeginTime = dataArr[12] + "-" + tomorrow;
    var numendTime = dataArr[0] + "-" + day;
    var valtime = [dataArr[12] + "-" + tomorrow, dataArr[0] + "-" + day];

近七日

    var dayone = "";
    var dayserven = "";
    let time = new Date();
    time.setTime(time.getTime());
    let Y1 = time.getFullYear();
    let M1 =
      time.getMonth() + 1 > 10
        ? time.getMonth() + 1
        : "0" + (time.getMonth() + 1);
    let D1 = time.getDate() > 10 ? time.getDate() : "0" + time.getDate();
    dayserven = Y1 + "-" + M1 + "-" + D1;
    console.log("dayserven", dayserven);
    let time2 = new Date();
    time2.setTime(time2.getTime() - 24 * 60 * 60 * 1000 * 6);
    let Y2 = time2.getFullYear();
    let M2 =
      time2.getMonth() + 1 > 9
        ? time2.getMonth() + 1
        : "0" + (time2.getMonth() + 1);
    let D2 = time2.getDate() > 9 ? time2.getDate() : "0" + time2.getDate();
    dayone = Y2 + "-" + M2 + "-" + D2;
    console.log("dayone", dayone);
    var valtime = [dayone, dayserven];
    var numbeginTime = dayone;
    var numendTime = dayserven;

当月

    var currentDate_mon = new Date();
    var date = new Date(currentDate_mon);
    date.setDate(1);
    var month = parseInt(date.getMonth() + 1);
    var day = date.getDate();
    if (month < 10) {
      month = "0" + month;
    }
    if (day < 10) {
      day = "0" + day;
    }
    var firstMouth = date.getFullYear() + "-" + month + "-" + day;
    var currentMonth = date.getMonth();
    var nextMonth = ++currentMonth;
    var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
    var oneDay = 1000 * 60 * 60 * 24;
    var lastTime = new Date(nextMonthFirstDay - oneDay);
    var lastmonth = parseInt(lastTime.getMonth() + 1);
    var lastday = lastTime.getDate();
    if (lastmonth < 10) {
      lastmonth = "0" + lastmonth;
    }
    if (lastday < 10) {
      lastday = "0" + lastday;
    }
    var endMouth = date.getFullYear() + "-" + lastmonth + "-" + lastday;
    var resumeBeginTime = firstMouth;
    var resumeEndTime = endMouth;
    var resumeTime = [firstMouth, endMouth];

近一个月

var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1; //0-11表示1-12月
    var day = date.getDate();
    if (parseInt(day) < 10) {
        day = "0" + day;
    }
    if (parseInt(month) < 10) {
        month = "0" + month;
    }
    var dateObj = {};
    dateObj.end = year + '-' + month + '-' + day;
    var endMonthDay = new Date(year, month, 0).getDate(); //当前月的总天数
    console.log(month)
    if (month - 1 <= 0) { //如果是1月,年数往前推一年<br>    
        dateObj.start = (year - 1) + '-' + 12 + '-' + day;
    } else {
        var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate();
        month = parseInt(month) - 1
        if (parseInt(month) < 10) {
            month = "0" + month;
        }
        if (startMonthDay < day) { //1个月前所在月的总天数小于现在的天日期
            if (day < endMonthDay) { //当前天日期小于当前月总天数
                dateObj.start = year + '-' + month + '-' + (startMonthDay - (endMonthDay - day));
            } else {
                dateObj.start = year + '-' + month + '-' + startMonthDay;
            }
        } else {
            dateObj.start = year + '-' + month + '-' + day;
        }
    }
    var valTime = [dateObj.start, dateObj.end]

posted @ 2020-05-22 14:51  漫漫码农路  阅读(284)  评论(0编辑  收藏  举报