一些常用的前端方法

一些常用的前端方法
日期format
/**

  • @param {string | Date} date 日期

  • @param {string | Array} format 分隔符 或 年月日

  • @return {string} 格式化日期 或 完整年月日日期
    */
    function formatDate(date, format = ‘-’) {
    if (!date) {
    return ‘’;
    }

    let year = ‘’;
    let month = ‘’;
    let day = ‘’;
    if (date instanceof Date) {
    year = date.getFullYear();

     month = date.getMonth() + 1;
     if (month < 10) {
         month = '0' + month;
     }
     
     day = date.getDate();
     if (day < 10) {
         day = '0' + day;
     }
    

    } else {
    const dateStr = date.replace(/[^0-9]/g, ‘’);

     year = dateStr.slice(0, 4);
     month = dateStr.slice(4, 6);
     day = dateStr.slice(6);
    

    }

    if (!Array.isArray(format)) {
    return year + format + month + (day ? format + day : ‘’);
    }

    const [yearStr, monthStr, dayStr] = format;
    return (yearStr ? year + yearStr : ‘’) +
    (monthStr ? month + monthStr : ‘’) +
    (dayStr ? day + dayStr : ‘’);
    }

// e.g.
formatDate(new Date(), ‘’); // 20231001
formatDate(new Date(), ‘-’); // 2023-10-01
formatDate(‘20231001’, ‘/’); // 2023/10/01

formatDate(new Date(), [‘年’, ‘月’, ‘日’]); // 2023年10月01日
formatDate(‘20231001’, [‘年’, ‘月’]); // 2023年10月
formatDate(‘20231001’, [‘’, ‘月’, ‘日’]); // 10月01日
时间format
/**

  • @param {Date} date 日期
  • @param {string} format 分隔符
  • @return {string} 格式化时间
    /
    function getHMS = (date: Date, format = ‘:’) {
    return [
    date.getHours().toString().padStart(2, ‘0’),
    date.getMinutes().toString().padStart(2, ‘0’),
    date.getSeconds().toString().padStart(2, ‘0’),
    ].join(format);
    }
    填充内容保留两位小数format
    /
    *
  • @param {string} value 需要保留两位小数的输入内容
  • @return 保留两位小数的字符串
    /
    function formatValueReg(value) {
    return value
    .replace(/[^\d.]/g, ‘’)
    .replace(‘.’, ‘KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲’)
    .replace(/./g, ‘’)
    .replace(‘KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲’, ‘.’)
    .replace(/.{2,}/g, ‘.’)
    .replace(/^(-)
    (\d+).(\d\d).*$/, ‘$1$2.$3’);
    }
    倒计时
    /**
  • @param {number} time 倒计时时长,单位s
  • @return
    */
    function countDown(time) {
    let t = time;
    let timer;
    return {
    minus: (callback) => {
    timer = setInterval(() => {
    t --;
    callback(t);
    if (t <= 0) {
    clearInterval(timer);
    timer = undefined;
    }
    }, 1000);
    },
    clear: () => {
    if (timer) {
    clearInterval(timer);
    timer = undefined;
    }
    }
    }
    }

// 使用示例
isSendingSms = true;
const cd = countDown(60);
cd.minus((t) => {
// 保证再次调用时,清除上一次的倒计时
if (!isSendingSms) {
cd.clear();
return;
}
smsCountDown = t;
if (t <= 0) {
reset(‘重新发送’);
}
});
获取相对某个日期的方法
/**

  • @param {Date} date 日期值
  • @param {number} year 增减年份,增为正,减为负
  • @param {number} month 增减月份,增为正,减为负
  • @param {number} day 增减日期,增为正,减为负
  • @return {Date} 日期
    */
    function getDate(date, year = 0, month = 0, day = 0) {
    // date格式不要携带 “-” 使用 “/” ,否则ios会报错。 例如 new Date(‘2023/07/28’)
    if (year) {
    date.setFullYear(date.getFullYear() + year);
    }
    if (month) {
    date.setMonth(date.getMonth() + month);
    }
    if (day) {
    date.setDate(date.getDate() + day);
    }
    return date;
    }

// e.g.
// i.e., today is Tue Aug 01 2023 10:26:25 GMT+0800 (中国标准时间)
getDate(new Date(), 1, 0, 0); // Tue Oct 01 2024 00:00:01 GMT+0800 (中国标准时间)
getDate(new Date(), 1, 1, 0); // Tue Nov 01 2023 00:00:01 GMT+0800 (中国标准时间)
getDate(new Date(), 1, 1, 1); // Tue Nov 02 2023 00:00:01 GMT+0800 (中国标准时间)
getDate(new Date(), -1, -1, -1); // Tue Aug 31 2022 00:00:01 GMT+0800 (中国标准时间)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值