-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
113 lines (109 loc) · 3.96 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
function DateCalc(date, bef, aft){
if(date){
this.date = [date.substr(0,4), '-', date.substr(4,2), '-', date.substr(-2)].join('')
}else{
var d = new Date();
this.date = [d.getFullYear(), '-', this._cover(d.getMonth()+1), '-', this._cover(d.getDate())].join('')
}
this.bef = bef || 0;
this.aft = aft || 0;
this.weekDayArr = ['Sun','Mon', 'Tues', 'Wen', 'Thur', 'Fri', 'Sat'];
this.weekDayCNArr = ['日','一', '二', '三', '四', '五', '六'];
this.monthArr = ['00','01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'];
this.monthENArr = ['','Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
}
DateCalc.prototype = {
constructor: DateCalc,
time: function(timestamp){
var d = timestamp ? new Date(parseInt(timestamp, 10)) : new Date();
var date = [d.getFullYear(), this._cover(d.getMonth()+1), this._cover(d.getDate())].join('');
return date + ' ' + [this._cover(d.getHours()), this._cover(d.getMinutes()), this._cover(d.getSeconds())].join(':')
},
now: function(date){
date && (this.date = [date.substr(0,4), '-', date.substr(4,2), '-', date.substr(-2)].join(''))
var d = this.date ? new Date(this.date) : new Date();
return [d.getFullYear(), this._cover(d.getMonth()+1), this._cover(d.getDate())].join('');
},
today: function(){
var d = new Date();
return [d.getFullYear(), this._cover(d.getMonth()+1), this._cover(d.getDate())].join('');
},
before: function(days){
return this._calc(days || 1,'before');
},
beforeCN: function(days){
return this.CHN(this._calc(days || 1,'before'))
},
after: function(days){
return this._calc(days || 1,'after');
},
afterCN: function(days){
return this.CHN(this._calc(days || 1,'after'))
},
month: function(){
var d = this.date ? new Date(this.date) : new Date();
return [d.getFullYear(), this._cover(d.getMonth()+1)].join('');
},
monthEN: function(dtime){
dtime = dtime ? dtime : this.now()
return this.monthENArr[parseInt(dtime.substr(4,2))];
},
beforeMonth: function(){
var y = parseInt(this.month().substr(0,4), 10),
m = this.month().substr(4,2),
idx = this.monthArr.indexOf(m);
if(idx == 1){
m = '12';
y--;
}else {
m = this.monthArr[idx-1];
}
return y + '' + m;
},
afterMonth: function(){
var y = parseInt(this.month().substr(0,4), 10),
m = this.month().substr(4,2),
idx = this.monthArr.indexOf(m);
if(idx == 12){
m = '01';
y++;
}else {
m = this.monthArr[idx+1];
}
return y + '' + m;
},
CHN: function(dtime){
dtime = dtime ? dtime : this.now()
var y = dtime.substr(0,4)+'年',
m = dtime.substr(4,2)+'月',
d = dtime.substr(6,2)+'日';
return y+m+d;
},
weekDay: function(dtime){
dtime = dtime ? dtime : this.now()
var day = new Date([dtime.substr(0,4), '-', dtime.substr(4,2), '-', dtime.substr(-2)].join('')).getDay();
return {
day: day,
en: this.weekDayArr[day],
cn: this.weekDayCNArr[day]
}
},
_calc: function(days, type){
var d = new Date(this.date),
input = 0;
if(type === 'before') {
input = 0 - this.bef;
days = 0 - days;
}else {
input = this.aft;
}
var total = days || input || 0;
var newDate = new Date(d.getTime() + 3600*24*1000*total);
return [newDate.getFullYear(), this._cover(newDate.getMonth()+1), this._cover(newDate.getDate())].join('');
},
_cover:function(num){
var n = parseInt(num, 10);
return n < 10 ? '0' + n : n;
}
}
module.exports = DateCalc