代码改变世界

获取本周,本月,本年,本季度 开始时间结束时间等等之类的

2022-10-27 09:47  WEB前端小菜鸟  阅读(166)  评论(0编辑  收藏  举报
      //获得本周的开始日期
      function getWeekStartDate() {
        let weekStartDate = new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          new Date().getDate() - new Date().getDay() + 1
        );
        return this.formatDate(weekStartDate);
      }
      //获得本周的结束日期
      function getWeekEndDate() {
        let weekEndDate = new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          new Date().getDate() + (7 - new Date().getDay())
        );
        return this.formatDate(weekEndDate);
      }
      //获得本年的开始日期
      function getYearStartDate() {
        //获得当前年份4位年
        let currentYear = new Date().getFullYear();
        //本年第一天
        let currentYearFirstDate = new Date(currentYear, 0, 1);
        return this.formatDate(currentYearFirstDate);
      }
      //获得本年的结束日期
      function getYearEndDate() {
        //获得当前年份4位年
        let currentYear = new Date().getFullYear();
        //本年最后
        let currentYearLastDate = new Date(currentYear, 11, 31);
        return this.formatDate(currentYearLastDate);
      }
      //获得本季度的开始日期
      function getQuarterStartDate() {
        let quarterStartDate = new Date(
          new Date().getFullYear(),
          this.getQuarterStartMonth(),
          1
        );
        return this.formatDate(quarterStartDate);
      }
      //获的本季度的结束日期
      function getQuarterEndDate() {
        let quarterEndMonth = this.getQuarterStartMonth() + 2;
        let quarterStartDate = new Date(
          new Date().getFullYear(),
          quarterEndMonth,
          this.getMonthDays(quarterEndMonth)
        );
        return this.formatDate(quarterStartDate);
      }
      //获得本季度的开始月份
      function getQuarterStartMonth() {
        let quarterStartMonth = 0;
        if (new Date().getMonth() < 3) {
          quarterStartMonth = 0;
        }
        if (2 < new Date().getMonth() && new Date().getMonth() < 6) {
          quarterStartMonth = 3;
        }
        if (5 < new Date().getMonth() && new Date().getMonth() < 9) {
          quarterStartMonth = 6;
        }
        if (new Date().getMonth() > 8) {
          quarterStartMonth = 9;
        }
        return quarterStartMonth;
      }
      //获得某月的天数 new Date().getMonth()不加1就是当月天数
      function getMonthDays(myMonth) {
        let monthStartDate = new Date(new Date().getFullYear(), myMonth, 1);
        let monthEndDate = new Date(new Date().getFullYear(), myMonth + 1, 1);
        let days =
          (Number(monthEndDate) - Number(monthStartDate)) /
          (1000 * 60 * 60 * 24);
        return days;
      }
      //获得本月的开始日期
      function getMonthStartDate() {
        let monthStartDate = new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          1
        );
        return this.formatDate(monthStartDate);
      }
      //获得本月的结束日期
      function getMonthEndDate() {
        let monthEndDate = new Date(
          new Date().getFullYear(),
          new Date().getMonth(),
          this.getMonthDays(new Date().getMonth())
        );
        return this.formatDate(monthEndDate);
      }
      //获得上月的开始日期
      function getLastMonthStartDate() {
        let nowYear = new Date().getFullYear();
        let lastMonthDate = new Date(); //上月日期
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        let lastMonth = lastMonthDate.getMonth();
        let lastMonthStartDate = new Date(nowYear, lastMonth, 1);
        return formatDate(lastMonthStartDate);
      }
      //获得上月的结束日期
      function getLastMonthEndDate() {
        let nowYear = new Date().getFullYear();
        let lastMonthDate = new Date(); //上月日期
        lastMonthDate.setDate(1);
        lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
        let lastMonth = lastMonthDate.getMonth();

        let lastMonthEndDate = new Date(
          nowYear,
          lastMonth,
          getMonthDays(lastMonth)
        );
        return formatDate(lastMonthEndDate);
      }
      // 格式化时间
      function formatDate(value) {
        let date = new Date(value);
        let y = date.getFullYear();
        let m = date.getMonth() + 1;
        m = m < 10 ? "0" + m : m;
        let d = date.getDate();
        d = d < 10 ? "0" + d : d;
        let time = y + "-" + m + "-" + d;
        return time;
      }