riku
5 天以前 f19e5267cc23b1c714dc746239864f33ed715dd9
src/utils/time-util.js
@@ -29,7 +29,15 @@
    }
  },
  formatYMDH(date) {
  formatYMDHM(date) {
    if (date) {
      return this.format(date, 'YYYY-MM-DD HH:mm');
    } else {
      return '----/--/-- --:--';
    }
  },
  formatYMDHMS(date) {
    if (date) {
      return this.format(date, 'YYYY-MM-DD HH:mm:ss');
    } else {
@@ -51,5 +59,47 @@
      format +
      (date < 10 ? '0' + date : date)
    );
  },
  /**
   * 将秒数转换为中文描述格式
   * @param {number} seconds - 秒数
   * @returns {string} 中文时间描述
   */
  formatSecondsToChinese(seconds) {
    if (!seconds || seconds < 0 || isNaN(seconds)) {
      return '--';
    }
    // 定义时间单位和对应的秒数
    const units = [
      { unit: '天', value: 24 * 60 * 60 },
      { unit: '小时', value: 60 * 60 },
      { unit: '分钟', value: 60 },
      { unit: '秒', value: 1 }
    ];
    let remainingSeconds = Math.floor(seconds);
    let result = '';
    // 遍历时间单位,计算每个单位的数量
    for (const { unit, value } of units) {
      if (remainingSeconds >= value) {
        const count = Math.floor(remainingSeconds / value);
        result += `${count}${unit}`;
        remainingSeconds %= value;
        // // 如果剩余秒数为0,且已经有结果,就可以结束了
        // if (remainingSeconds === 0 && result) {
        //   break;
        // }
        // 如果已经有结果,就可以结束了
        if (result) {
          break;
        }
      }
    }
    return result;
  }
};
};