欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

给出年月值,获取月末和月初值

程序员文章站 2022-05-15 09:16:09
...
 var date = this.filters.month;
      if (!date) {
        this.$message({
          type: "info",
          message: "请选择日期"
        });
        return;
      }
      date = new Date(date);
      var startDate = "",
        endDate = "";
      startDate =
        date.getFullYear() +
        "-" +
        (date.getMonth() + 1) +
        "-" +
        "01" +
        " " +
        "00:00:00";
      if (date.getMonth() == 11) {
        return {
          startDate: startDate,
          endDate:
            date.getFullYear() +
            "-" +
            (date.getMonth() + 1) +
            "-" +
            "31" +
            " " +
            "23:59:59"
        };
      }
      var currentMonth = date.getMonth() + 1;
      var nextMonth = ++currentMonth;
      var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
      var oneDay = 1000 * 60 * 60 * 24;
      var dates = new Date(nextMonthFirstDay - oneDay);
      var day = "";

      switch (dates.getMonth()) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
          day = "31";
          break;
        case 2:
          if (dates.getFullYear % 4 == 0) {
            day = "29";
          } else {
            day = "28";
          }
          break;
        case 4:
        case 6:
        case 9:
        case 11:
          day = "30";
          break;
      }
      endDate =
        dates.getFullYear() + "-" + dates.getMonth() + "-" + day + " 23:59:59";

月初很简单,直接给,月末的话就需要计算一下了,然后还要判断当前月是几月,从而日期给予确定