1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | export default function formatDuration(ms) {
|
8 | if (ms < 0) ms = -ms;
|
9 | return {
|
10 | day: Math.floor(ms / 86400000),
|
11 | hour: Math.floor(ms / 3600000) % 24,
|
12 | minute: Math.floor(ms / 60000) % 60,
|
13 | second: Math.floor(ms / 1000) % 60,
|
14 | millisecond: Math.floor(ms) % 1000,
|
15 | };
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | export function formatDurationToMinute(ms) {
|
24 | const result = [];
|
25 | const o = formatDuration(ms);
|
26 | const times = [
|
27 | [o.day, "天"],
|
28 | [o.hour, "小时"],
|
29 | [o.minute, "分"],
|
30 | ];
|
31 | for (let item of times) {
|
32 | const [value, label] = item;
|
33 | if (value > 0) {
|
34 | result.push(`${value}${label}`);
|
35 | }
|
36 | }
|
37 | return result.join("");
|
38 | }
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | export function formatDurationToSecond(ms) {
|
46 | const result = [];
|
47 | const o = formatDuration(ms);
|
48 | const times = [
|
49 | [o.day, "天"],
|
50 | [o.hour, "小时"],
|
51 | [o.minute, "分"],
|
52 | [o.second, "秒"],
|
53 | ];
|
54 | for (let item of times) {
|
55 | const [value, label] = item;
|
56 | if (value > 0) {
|
57 | result.push(`${value}${label}`);
|
58 | }
|
59 | }
|
60 | return result.join("");
|
61 | }
|