forked from Adivise/EasyHost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertTime.js
66 lines (58 loc) · 2.11 KB
/
ConvertTime.js
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
module.exports = {
convertTime: function(duration) {
var milliseconds = parseInt((duration % 1000) / 100),
seconds = parseInt((duration / 1000) % 60),
minutes = parseInt((duration / (1000 * 60)) % 60),
hours = parseInt((duration / (1000 * 60 * 60)) % 24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
if (duration < 3600000) {
return minutes + ":" + seconds;
} else {
return hours + ":" + minutes + ":" + seconds;
}
},
convertNumber: function(number, decPlaces) {
decPlaces = Math.pow(10, decPlaces);
var abbrev = ["K", "M", "B", "T"];
for (var i = abbrev.length - 1; i >= 0; i--) {
var size = Math.pow(10, (i + 1) * 3);
if (size <= number) {
number = Math.round(number * decPlaces / size) / decPlaces;
if ((number == 1000) && (i < abbrev.length - 1)) {
number = 1;
i++;
}
number += abbrev[i];
break;
}
}
return number;
},
chunk: function(arr, size) {
const temp = [];
for (let i = 0; i < arr.length; i += size) {
temp.push(arr.slice(i, i + size));
}
return temp;
},
convertHmsToMs: function(hms) {
if (hms.length < 3) {
return hms = ((+a[0]) * 1000)
} else if (hms.length < 6) {
const a = hms.split(':')
return hms = (((+a[0]) * 60 + (+a[1])) * 1000)
} else {
const a = hms.split(':')
return hms = (((+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2])) * 1000)
}
},
convertSeconds: function(seconds) {
var hours = Math.floor(seconds / 3600);
seconds %= 3600;
var minutes = Math.floor(seconds / 60);
seconds = Math.floor(seconds % 60);
return (hours ? hours + ":" + (minutes < 10 ? "0" : "") : "") + minutes + ":" + (seconds < 10 ? "0" : "") + seconds;
}
}