关于时间的一些方法

 获取当前月的天数

    getNowMonthDay(month) = > {

      let date = new Date();

      let year = date.getFullYear();

      let days = new Date(year, month, 0).getDate();

      console.log(days)

    },

获取指定年份的周数并格式化   ['1周(1.1-1.7)' , '2周(1.8-1.14)' , '3周(1.15-1.21)' .... ]

getFormattedWeeksInYear(year)=> {
      const firstDayOfYear = new Date(year, 0, 1);
      const daysToFirstThursday = (4 - firstDayOfYear.getDay() + 7) % 7;
      const firstThursday = new Date(year, 0, 1 + daysToFirstThursday);
      const weeksInYear =
        Math.ceil(
          (new Date(year, 11, 31) - firstThursday) / (7 * 24 * 60 * 60 * 1000)
        ) + 1;
      // 格式化展示周数数组
      let weeks = [];
      for (let week = 1; week <= weeksInYear; week++) {
        const start = this.getDateOfISOWeek(year, week);
        const end = this.getDateOfISOWeek(year, week, 6);
        const formattedWeek = `${week}周 (${start.getMonth() + 1}.${start.getDate()} - ${end.getMonth() + 1}.${end.getDate()})`;
        weeks.push(formattedWeek);
      }
      console.log(weeks)
      weeks = [];
    };
     获取指定年份和周数的日期
    getDateOfISOWeek(year, week, day = 0) =>{
      const date = new Date(year, 0, 1 + (week - 1) * 7);
      const dayOffset = date.getDay() - 1;
      return new Date(date.setDate(date.getDate() - dayOffset + day));
    },

 判断今天属于哪一周

getWeekNumber() =>{
      const today = new Date();
      const formattedToday = `${today.getMonth() + 1}.${today.getDate()}`;
      for (let i = 0; i < this.weeks.length; i++) {
        const weekRange = this.weeks[i].match(/\d+\.\d+/g);
        const start = weekRange[0];
        const end = weekRange[1];
        if (formattedToday >= start && formattedToday <= end) {
          this.active = i;
        }
      }
      return null; // 今天不属于任何一周
    }

时间戳转换成yy-mm-dd

 formatDate(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = (date.getMonth() + 1).toString().padStart(2, "0");
      const day = date.getDate().toString().padStart(2, "0");
      return `${year}-${month}-${day}`;
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值