JS时间对象常用函数与日期转换

时间对象常用函数

// 获取指定年份最后一天
      function getLastDay(year, month) {
        var new_year = year; //取当前的年份
        var new_month = month++; //取下一个月的第一天,方便计算(最后一天不固定)
        if (month > 12) {
          new_month -= 12; //月份减
          new_year++; //年份增
        }
        var new_date = new Date(new_year, new_month, 1); //取当年当月中的第一天
        return new Date(new_date.getTime() - 1000 * 60 * 60 * 24).getDate(); //获取当月最后一天日期
      }
// 获取指定日期下一天
      function nextDate(date) {
        date = new Date(date);
        date = +date + 1000 * 60 * 60 * 24;
        date = new Date(date);
        return (
          date.getFullYear() +
          "-" +
          ((date.getMonth() + 1).toString().length == 1
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) +
          "-" +
          (date.getDate().toString().length == 1
            ? "0" + date.getDate()
            : date.getDate())
        );
      }

日期转换函数

// 转换日期格式
    changeFormat(v) {
      if (v) {
        const time = new Date(v);
        return (
          time.getFullYear() +
          "." +
          (time.getMonth() + 1).toString().padStart(2, 0) +
          "." +
          time.getDate().toString().padStart(2, 0) +
          " " +
          time.getHours().toString().padStart(2, 0) +
          ":" +
          time.getMinutes().toString().padStart(2, 0) +
          ":" +
          time.getSeconds().toString().padStart(2, 0)
        );
      } else {
        return "无";
      }
    },

 可以参考使用 day.js插件

JS时间对象使用之按月与日期划分时间段

场景:传入三个参数2020-02-02,2020-08-01,31,开始日期,结束日期,分隔日期

要求:从开始日期到结束日期,按每个月31号划分成日期段(不足的自动按照当月最后一天)

dataMonth(value1, value2, day) {
      var getDate = function (str) {
        var tempDate = new Date();
        var list = str.split("-");
        tempDate.setFullYear(list[0] * 1);
        tempDate.setDate(1);
        tempDate.setMonth(list[1] * 1 - 1);
        tempDate.setDate(list[2] * 1);
        return tempDate;
      };
      var date1 = getDate(value1);
      var date2 = getDate(value2);
      var dateArr = new Array();
      var i = 0;
      dateArr[i] = new Array();
      if (!day) {
        dateArr[i] = [value1, 0];
        i++;
      } else {
        dateArr[i][0] = value1;
      }
      date1.setDate(1);
      date1.setMonth(date1.getMonth() + 1);
      while (
        !(
          date1.getFullYear() > date2.getFullYear() ||
          (date1.getFullYear() == date2.getFullYear() &&
            date1.getMonth() > date2.getMonth())
        )
      ) {
        // console.log(1111, getLastDay(date1.getFullYear(), date1.getMonth() + 1))
        let lastDay = getLastDay(date1.getFullYear(), date1.getMonth() + 1);
        if (!day) {
          dateArr[i] = [
            date1.getFullYear() +
              "-" +
              ((date1.getMonth() + 1).toString().length == 1
                ? "0" + (date1.getMonth() + 1)
                : date1.getMonth() + 1) +
              "-0" +
              1,
            0,
          ];
        } else {
          dateArr[i + 1] = new Array();
          let currentDay =
            date1.getFullYear() +
            "-" +
            ((date1.getMonth() + 1).toString().length == 1
              ? "0" + (date1.getMonth() + 1)
              : date1.getMonth() + 1) +
            "-" +
            ((day * 1).toString().length == 1 ? "0" + day : day);
          if (day * 1 > lastDay * 1) {
            currentDay =
              date1.getFullYear() +
              "-" +
              ((date1.getMonth() + 1).toString().length == 1
                ? "0" + (date1.getMonth() + 1)
                : date1.getMonth() + 1) +
              "-" +
              lastDay;
          }
          dateArr[i][1] = currentDay;
          let next = getDate(currentDay);
          // console.log(2222, currentDay, next)
          let nextDay = nextDate(next);
          dateArr[i + 1][0] = nextDay;
          // console.log(111, getDate(dateArr[i + 1][0]))
        }
        i++;
        // date1.setDate(date1.getDate() + 1);
        date1.setMonth(date1.getMonth() + 1);
      }
      // console.log(111, date1)
      if (day) {
        let lastDay = getLastDay(date1.getFullYear(), date1.getMonth() + 1);
        let currentDay =
          date1.getFullYear() +
          "-" +
          ((date1.getMonth() + 1).toString().length == 1
            ? "0" + (date1.getMonth() + 1)
            : date1.getMonth() + 1) +
          "-" +
          ((day * 1).toString().length == 1 ? "0" + day : day);
        if (day * 1 > lastDay * 1) {
          currentDay =
            date1.getFullYear() +
            "-" +
            ((date1.getMonth() + 1).toString().length == 1
              ? "0" + (date1.getMonth() + 1)
              : date1.getMonth() + 1) +
            "-" +
            lastDay;
        }
        dateArr[i][1] = currentDay;
      }
      console.log("dateArr", dateArr);
      return dateArr;
      // 获取指定日期下一天
      function nextDate(date) {
        date = new Date(date);
        date = +date + 1000 * 60 * 60 * 24;
        date = new Date(date);
        return (
          date.getFullYear() +
          "-" +
          ((date.getMonth() + 1).toString().length == 1
            ? "0" + (date.getMonth() + 1)
            : date.getMonth() + 1) +
          "-" +
          (date.getDate().toString().length == 1
            ? "0" + date.getDate()
            : date.getDate())
        );
      }
      // 获取指定年份最后一天
      function getLastDay(year, month) {
        var new_year = year; //取当前的年份
        var new_month = month++; //取下一个月的第一天,方便计算(最后一天不固定)
        if (month > 12) {
          new_month -= 12; //月份减
          new_year++; //年份增
        }
        var new_date = new Date(new_year, new_month, 1); //取当年当月中的第一天
        return new Date(new_date.getTime() - 1000 * 60 * 60 * 24).getDate(); //获取当月最后一天日期
      }
    },

结果:

注意点:

let date=date1.getDate()
date1.setDate(1); date1.setMonth(date1.getMonth() + 1);
data1.setDate(date)

设置为下一个月,当下一个月当前日期没有时,即下月最大天数不够时,月份会往下多加一个月,此时需要先setDate(1),然后设置为下一个月,设置好月份之后再setDate(date)

posted @ 2020-08-01 15:50  盼星星盼太阳  阅读(208)  评论(0编辑  收藏  举报