-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval.js
58 lines (53 loc) · 1.8 KB
/
interval.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
const repeatInterval = (remind_keyword, sequence, time_interval, name, duration) => {
switch (remind_keyword) {
case 'secondly':
return [time_interval, 1, name, sequence, 1 * 1000, duration];
case 'minutely':
return [time_interval, 1, name, sequence, 60 * 1000, duration];
case 'daily':
return [time_interval, 1, name, sequence, 24 * 3600 * 1000, duration];
case 'hourly':
return [time_interval, 1, name, sequence, 3600 * 1000, duration];
case 'once':
return [time_interval, 0, name, sequence, 0, duration];
}
}
const calcInterval = (datetime) => {
let etime = new Date(datetime);
let e_hr = etime.getHours();
let e_min = etime.getMinutes();
let e_sec = etime.getSeconds();
let curtime = new Date();
let cur_hr = curtime.getHours();
let cur_min = curtime.getMinutes();
let cur_sec = curtime.getSeconds();
let interval = ((e_hr - cur_hr) * 3600 + (e_min - cur_min) * 60 + (e_sec - cur_sec)) * 1000;
return interval;
}
const timeSpent = (d) => {
let interval = calcInterval(d) * -1;
let total = interval / 1000;
let hr = Math.floor(total / 3600);
let min = Math.floor((total % 3600) / 60);
let sec = (total % 3600) % 60;
return "You've spent " + hr.toString() + " hours " + min.toString() + " minutes " + sec.toString() + " seconds"
}
const addZero = (t) => {
if (t < 10) {
t = "0" + t;
}
return (t);
}
const getTime = (datetime) => {
let etime = new Date(datetime);
let e_hr = addZero(etime.getHours());
let e_min = addZero(etime.getMinutes());
let time = e_hr + ":" + e_min;
return time;
}
module.exports = {
calcInterval: calcInterval,
repeatInterval: repeatInterval,
timeSpent: timeSpent,
getTime: getTime
}