riku
5 天以前 f19e5267cc23b1c714dc746239864f33ed715dd9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import dayjs from 'dayjs';
 
export default {
  format(date, template) {
    return dayjs(date).format(template);
  },
 
  formatH(date) {
    if (date) {
      return this.format(date, 'HH:mm:ss');
    } else {
      return '--:--:--';
    }
  },
 
  formatYM(date) {
    if (date) {
      return this.format(date, 'YYYY-MM');
    } else {
      return '----/--';
    }
  },
 
  formatYMD(date) {
    if (date) {
      return this.format(date, 'YYYY-MM-DD');
    } else {
      return '----/--/--';
    }
  },
 
  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 {
      return '----/--/-- --:--:--';
    }
  },
 
  formatDateFromExcel(num, format) {
    const old = num - 1;
    const t = Math.round((old - Math.floor(old)) * 24 * 60 * 60);
    const time = new Date(1900, 0, old, 0, 0, t);
    const year = time.getFullYear();
    const month = time.getMonth() + 1;
    const date = time.getDate();
    return dayjs(
      year +
      format +
      (month < 10 ? '0' + month : month) +
      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;
  }
};