vuejs获取指定日期的那一周的开始、结束日期(从


<script>
export default {
  data() {
    return {
      weekFormat: ""
    };
  },
  mounted() {
    console.log(this.getWeekStartAndEnd("2021-5-25"));
  },
  methods: {
    // 日期格式化
    formatDate(date) {
      var myyear = date.getFullYear();
      var mymonth = date.getMonth() + 1;
      var myweekday = date.getDate();
      if (mymonth < 10) {
        mymonth = "0" + mymonth;
      }
      if (myweekday < 10) {
        myweekday = "0" + myweekday;
      }
      return mymonth + "." + myweekday;
    },
    // 获取指定日期的那一周的开始、结束日期
    getWeekStartAndEnd(val) {
      let now = "";
      if (val) {
        now = new Date(val); // 日期
      } else {
        now = new Date(); // 日期
      }
      let nowDayOfWeek = now.getDay(); // 本周的第几天
      let nowDay = now.getDate(); // 当前日
      let nowMonth = now.getMonth(); // 当前月
      let nowYear = now.getYear(); // 当前年
      let weekStart = this.getWeekStartDate(
        nowYear,
        nowMonth,
        nowDay,
        nowDayOfWeek
      );
      let weekEnd = this.getWeekEndDate(
        nowYear,
        nowMonth,
        nowDay,
        nowDayOfWeek
      );
      this.weekFormat = weekStart + "~" + weekEnd;
      console.log("weekFormat", this.weekFormat);
    },
    // 获得某一周的开始日期
    getWeekStartDate(nowYear, nowMonth, nowDay, nowDayOfWeek) {
      let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
      return this.formatDate(weekStartDate);
    },
    // 获得某一周的结束日期
    getWeekEndDate(nowYear, nowMonth, nowDay, nowDayOfWeek) {
      let weekEndDate = new Date(
        nowYear,
        nowMonth,
        nowDay + (6 - nowDayOfWeek)
      );
      return this.formatDate(weekEndDate);
    }
  }
};
</script>
以上是编程学习网小编为您介绍的“vuejs获取指定日期的那一周的开始、结束日期(从”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。

本文标题为:vuejs获取指定日期的那一周的开始、结束日期(从

基础教程推荐