-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.js
95 lines (73 loc) · 1.77 KB
/
time.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
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
module.exports = {
now : function(g){
let d;if(g){d = new Date(g);} else {d = new Date();}
return d.getTime();
},
date : function(g){
let d;if(g){d = new Date(g);} else {d = new Date();}
return d.getDate();
},
month : function(g){
let d;if(g){d = new Date(g);} else {d = new Date();}
return (d.getMonth() + 1);
},
year : function(g){
let d;if(g){d = new Date(g);} else {d = new Date();}
return (d.getFullYear());
},
day : function(g){
let d;if(g){d = new Date(g);} else {d = new Date();}
return (d.getDay() + 1);
},
diff : {
days : function(time1,time2){
let
aHold = new Date(time1),
bHold = new Date(time2),
a,b;
a = {
day:aHold.getDate(),
month:aHold.getMonth(),
year:aHold.getFullYear(),
hour:aHold.getHours(),
minutes:aHold.getMinutes(),
seconds:aHold.getSeconds()
};
b = {
day:bHold.getDate(),
month:bHold.getMonth(),
year:bHold.getFullYear(),
hour:bHold.getHours(),
minutes:bHold.getMinutes(),
seconds:bHold.getSeconds()
};
let base = 0;
//check if same year
if(a.year !== b.year){
let yearDiff = b.year - a.year;
base += yearDiff * 365;
}
//check month
if(a.month !== b.month){
let monthDiff = b.month - a.month;
base += monthDiff * 30;
}
//check days
if(a.day !== b.day){
let dayDiff = b.day - a.day;
if(dayDiff > 0){
base += dayDiff;
}
}
if(base > 0){
return base;
}
//check hours
if(a.hour !== b.hour){
let hourDiff = b.hour - a.hour;
base += hourDiff / 100;
}
return base;
}
}
};