This repository was archived by the owner on Sep 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmsToTime.js
More file actions
48 lines (39 loc) · 1.3 KB
/
msToTime.js
File metadata and controls
48 lines (39 loc) · 1.3 KB
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
function msToTime(duration) {
const portions = [];
const msInYear = 1000 * 60 * 60 * 24 * 365;
const years = Math.trunc(duration / msInYear);
if (years > 0) {
portions.push(`${years} year${years > 1 ? 's' : ''}`);
duration -= years * msInYear;
}
const msInMonth = 1000 * 60 * 60 * 24 * 30;
const months = Math.trunc(duration / msInMonth);
if (months > 0) {
portions.push(months + ' month'+(months > 1 ? 's' : ''));
duration -= months * msInMonth;
}
const msInDay = 1000 * 60 * 60 * 24;
const days = Math.trunc(duration / msInDay);
if (days > 0) {
portions.push(days + 'd');
duration = duration - (days * msInDay);
}
const msInHour = 1000 * 60 * 60;
const hours = Math.trunc(duration / msInHour);
if (hours > 0) {
portions.push(hours + 'h');
duration = duration - (hours * msInHour);
}
const msInMinute = 1000 * 60;
const minutes = Math.trunc(duration / msInMinute);
if (minutes > 0) {
portions.push(minutes + 'm');
duration = duration - (minutes * msInMinute);
}
const seconds = Math.trunc(duration / 1000);
if (seconds > 0) {
portions.push(seconds + 's');
}
return portions[0] ? portions[0] : 'Just now';
}
module.exports = msToTime;