-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.js
More file actions
70 lines (55 loc) · 1.69 KB
/
convert.js
File metadata and controls
70 lines (55 loc) · 1.69 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Convert module converts dates from one form to another.
convert.unixtonatural(unix in seconds) returns a natural date
convert.naturaltounix(natural) returns a unix date in seconds
*/
var convert = {
year: 0,
day: 0,
month: "",
dom: 1,
natural: "",
unix: 0,
noLeap: [1, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334],
leap: [1, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335],
montharr: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
monthindex: 0,
isleap: false,
unixtonatural: function(unix){
this.unix = unix;
this.year = Math.floor((this.unix / 31557600) + 1970);
this.day = Math.round((this.unix % 31557600)/86400) + 1;
if(this.year % 4 == 0){
this.isleap = true;
}
else{
this.isleap = false;
};
if(this.isleap == true){
for(var i = 0; i <= 11; i++){
if(this.leap[i] <= this.day){
this.monthindex = i;
}
}
this.month = this.montharr[this.monthindex];
this.dom = (this.day - this.leap[this.monthindex]) + 1;
}
else{
for(var i = 0; i <= 11; i++){
if(this.noLeap[i] <= this.day){
this.monthindex = i;
}
}
this.month = this.montharr[this.monthindex];
this.dom = (this.day - this.noLeap[this.monthindex]) + 1;
};
this.natural = this.month + " " + this.dom + ", " + this.year;
return this.natural;
},
naturaltounix: function(natural){
this.natural = natural;
this.unix = Date.parse(this.natural)/1000;
return this.unix;
}
};
module.exports = convert;