js小工具

判断数据类型 

/**
 * 判断数据的数据类型
 * "[object Null]" "[object Undefined]" "[object String]" "[object Number]" "[object Boolean]"
 * "[object Function]" "[object Date]" "[object Array]" "[object RegExp]" "[object Object]" 
 */
  function getDataType(data) {
      return Object.prototype.toString.call(data);
}

时间格式化

/**
 * 时间格式化
 * new Date().Format("yyyy-MM dd:HH,mm+ss") 返回格式 年-月 日:时,分+秒
 * Format()内传入你所需要的格式即可
 */

Date.prototype.Format = function (fmt) {
  const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']
  var o = {
    "M+": this.getMonth() + 1, // 月
    "W+": weekdays[this.getDay()], // 周
    "d+": this.getDate(), // 日
    "H+": this.getHours(), // 时
    "m+": this.getMinutes(), // 分
    "s+": this.getSeconds(), // 秒
    "q+": Math.floor((this.getMonth() + 3) / 3), // 季度
    "S+": this.getMilliseconds(), // 毫秒
  };
  if (fmt) {
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        (this.getFullYear() + "").substr(4 - RegExp.$1.length)
      );
    }
    for (var k in o) {
      if (new RegExp("(" + k + ")").test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          RegExp.$1.length == 1
            ? o[k]
            : ("00" + o[k]).substr(("" + o[k]).length)
        );
      }
    }
  } else {
    return this;
  }
  return fmt;
};

注册校验

注册校验:
C:\Users\用户名\AppData\Roaming\
Scooter Software\Beyond Compare 4
下的所有文件删掉,重启Beyond Compare 4即可
(注意:用户名下的AppData文件夹有可能会被隐藏起来)

根据开始时间,间隔天数回显结束时间

/**
 * 根据开始时间,间隔天数回显结束时间
 * date为相加前的时间, days 为需要相加的天数
 */

function addDate(datePra, days) {
  let date = new Date(datePra);
  date.setDate(date.getDate() + days);
  let year = date.getFullYear();
  let month = date.getMonth() + 1;
  let day = date.getDate();
  // 判断月、日是否需要补位 '0'
  if (month < 10) {
    month = '0' + month;
  }
  if (day < 10) {
    day = "0" + day;
  }
  let time = year + "-" + month + "-" + day;
  return time;
}

根据开始时间,结束时间计算间隔天数

/**
 * 根据开始时间,结束时间计算间隔天数
 * date为相加前的时间, days 为需要相加的天数
 */

function getDaysBetweenDates(startDate, endDate) {
  const oneDay = 24 * 60 * 60 * 1000; // 每天的毫秒数
  const startTime = new Date(startDate).getTime(); // 将字符串转换为时间戳
  const endTime = new Date(endDate).getTime(); // 将字符串转换为时间戳
  return Math.round((endTime - startTime) / oneDay); // 计算天数
}

获取指定日期区间的所有日期

/**
 * 获取指定日期区间的所有日期
 * 入参:2024-09-18 2024-09-28
 * 返回值:['2024-09-18','2024-09-19','2024-09-20']
 */

function getDatesInRange(startDate, endDate, type = 'yyyy-MM-dd') {
  let dates = [];
  let currentDate = new Date(startDate);
  while (currentDate <= new Date(endDate)) {
    dates.push(new Date(currentDate).Format(type));
    currentDate.setDate(currentDate.getDate() + 1);
  }
  return dates;
}

获取指定天所在当年的索引值位置 - 从0开始

/**
 * 获取指定天所在当年的索引值位置 - 从0开始
 * @param {Date、String} date 
 * @returns 索引位置
 */

function getDayIdx(date = new Date()) {
  let startOfYear = new Date(new Date(date).getFullYear(), 0, 1); // 获取年份的第一天
  let diff = date - startOfYear; // 计算日期和年份第一天的时间差(毫秒)
  return Math.floor(diff / (1000 * 60 * 60 * 24)); // 转换为天数
}

防抖函数

/**
 * @description:防抖函数
 * @param {Function} func 
 * @param {Number} delay 
 * @param {Boolean} immediate 
 * @returns {*}
 */

function debounce(func, delay, immediate = false) {
  let timer, context = this
  return (...args) => {
    if (immediate) {
      func.apply(context, args)
      immediate = false;
      return;
    }
    clearTimeout(timer);
    timer = setTimeout(() => {
      func.apply(context, args)
    }, delay);
  }
}

根据字符串的长度及类型自动计算合适的渲染尺寸

/**
 * 根据字符串的长度及类型自动计算合适的渲染尺寸
 * @param {String} str 
 * @param {Number} width 
 * @returns 
 */

function getWidthOfStrType(str, width = 30) {
  let chineseCharCount = 0, otherCharCount = 0
  if (Object.prototype.toString.call(str) == '[object String]') {
    if (str.length > 0) {
      const chineseRegex = /[\u4E00-\u9FFF]/g;
      let match;
      while ((match = chineseRegex.exec(str)) !== null) {
        chineseCharCount++;
      }
      otherCharCount = str.length - chineseCharCount
    }
  } else {
    console.error('请参入字符串类型--->' + str)
  }
  return chineseCharCount * width + otherCharCount * 10
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值