常用函数汇总(个人向,更新中)

防抖

function debounce(func: any, delay: any) {

      let timer: any = null;

      return function () {

        if (timer) {

          clearTimeout(timer);

          timer = null;

        }

        timer = setTimeout(() => {

          func(...arguments);

        }, delay);

      };

    }

  debounce(this.isClear(val),1000) // 参数1:需要防抖触发的函数  参数2:防抖的时间

时间戳转年月日时分秒

 function timestampToTime(timestamp: any) {
      var date = new Date(timestamp);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      var hours = date.getHours();
      var minutes = date.getMinutes();
      var seconds = date.getSeconds();
      return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
   }

获取当前时间是这个月的第几周

getWeekNum(value:any){
        const startDate = new Date(value); // 假设value是'yyyy-MM-dd'格式的字符串
        const firstDayOfMonth = new Date(startDate.getFullYear(), startDate.getMonth(), 1);
        const monthStartWeekday = firstDayOfMonth.getDay() || 7; // 如果周日是0,则转换为7进行计算
        const weekOfMonth = Math.ceil((startDate.getDate() + monthStartWeekday - 1) / 7);
        return weekOfMonth;
    }

获取当前周的所有日期

getDatesOfCurrentWeek(date?:any) {
        const now = date ? new Date(date) : new Date();
        const dayOfWeek = now.getDay(); // 0 表示星期天,1 到 6 表示星期一到星期六
        const startDate = new Date(now);
        startDate.setDate(startDate.getDate() - dayOfWeek + 1); // 本周的第一天 (星期一)
       
        const dates = [];
        for (let i = 0; i < 7; i++) {
          const date = new Date(startDate);
          date.setDate(startDate.getDate() + i);
          dates.push(date.toISOString().split('T')[0]); // 格式化日期为 YYYY-MM-DD
        }
        dates.forEach((val:any,index:any)=>{
            let arr = val.split('-')
            this.dateValues[index].clabel = `${arr[1]}月${arr[2]}日`
        })
      }

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值