uni-app 不能格式化超过1小时的时间

uni-app 不能格式化超过1小时的时间

2021-09-27 08:48

超过一小时的时间,直接显示原字符串

1 回复

更多关于uni-app 不能格式化超过1小时的时间的实战教程也可以访问 https://www.itying.com/category-93-b0.html


在uni-app中处理时间格式化时,如果遇到超过1小时的时间显示为原字符串的问题,通常是因为格式化函数或组件对时间格式的处理不够完善。以下是几种常见解决方案:

  1. 使用自定义格式化函数
    可以编写一个自定义的时间格式化函数,确保能正确处理超过1小时的时间。例如:

    function formatDuration(seconds) {
      const hours = Math.floor(seconds / 3600);
      const minutes = Math.floor((seconds % 3600) / 60);
      const secs = seconds % 60;
      return `${hours.toString().padStart(2, '0')}:${minutes.toString().padStart(2, '0')}:${secs.toString().padStart(2, '0')}`;
    }
    
  2. 检查第三方库的配置
    如果使用如moment.jsdayjs等库,需确认其格式化规则是否支持长时段时间。例如:

    // 使用dayjs的duration插件
    import dayjs from 'dayjs';
    import duration from 'dayjs/plugin/duration';
    dayjs.extend(duration);
    const formattedTime = dayjs.duration(seconds, 'seconds').format('HH:mm:ss');
回到顶部