date-arithmetic.js
4.2 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var MILI = 'milliseconds'
, SECONDS = 'seconds'
, MINUTES = 'minutes'
, HOURS = 'hours'
, DAY = 'day'
, WEEK = 'week'
, MONTH = 'month'
, YEAR = 'year'
, DECADE = 'decade'
, CENTURY = 'century';
var dates = module.exports = {
add: function(date, num, unit) {
date = new Date(date)
switch (unit){
case MILI:
case SECONDS:
case MINUTES:
case HOURS:
case YEAR:
return dates[unit](date, dates[unit](date) + num)
case DAY:
return dates.date(date, dates.date(date) + num)
case WEEK:
return dates.date(date, dates.date(date) + (7 * num))
case MONTH:
return monthMath(date, num)
case DECADE:
return dates.year(date, dates.year(date) + (num * 10))
case CENTURY:
return dates.year(date, dates.year(date) + (num * 100))
}
throw new TypeError('Invalid units: "' + unit + '"')
},
subtract: function(date, num, unit) {
return dates.add(date, -num, unit)
},
startOf: function(date, unit, firstOfWeek) {
date = new Date(date)
switch (unit) {
case 'century':
case 'decade':
case 'year':
date = dates.month(date, 0);
case 'month':
date = dates.date(date, 1);
case 'week':
case 'day':
date = dates.hours(date, 0);
case 'hours':
date = dates.minutes(date, 0);
case 'minutes':
date = dates.seconds(date, 0);
case 'seconds':
date = dates.milliseconds(date, 0);
}
if (unit === DECADE)
date = dates.subtract(date, dates.year(date) % 10, 'year')
if (unit === CENTURY)
date = dates.subtract(date, dates.year(date) % 100, 'year')
if (unit === WEEK)
date = dates.weekday(date, 0, firstOfWeek);
return date
},
endOf: function(date, unit, firstOfWeek){
date = new Date(date)
date = dates.startOf(date, unit, firstOfWeek)
date = dates.add(date, 1, unit)
date = dates.subtract(date, 1, MILI)
return date
},
eq: createComparer(function(a, b){ return a === b }),
neq: createComparer(function(a, b){ return a !== b }),
gt: createComparer(function(a, b){ return a > b }),
gte: createComparer(function(a, b){ return a >= b }),
lt: createComparer(function(a, b){ return a < b }),
lte: createComparer(function(a, b){ return a <= b }),
min: function(){
return new Date(Math.min.apply(Math, arguments))
},
max: function(){
return new Date(Math.max.apply(Math, arguments))
},
inRange: function(day, min, max, unit){
unit = unit || 'day'
return (!min || dates.gte(day, min, unit))
&& (!max || dates.lte(day, max, unit))
},
milliseconds: createAccessor('Milliseconds'),
seconds: createAccessor('Seconds'),
minutes: createAccessor('Minutes'),
hours: createAccessor('Hours'),
day: createAccessor('Day'),
date: createAccessor('Date'),
month: createAccessor('Month'),
year: createAccessor('FullYear'),
decade: function (date, val) {
return val === undefined
? dates.year(dates.startOf(date, DECADE))
: dates.add(date, val + 10, YEAR);
},
century: function (date, val) {
return val === undefined
? dates.year(dates.startOf(date, CENTURY))
: dates.add(date, val + 100, YEAR);
},
weekday: function (date, val, firstDay) {
var weekday = (dates.day(date) + 7 - (firstDay || 0) ) % 7;
return val === undefined
? weekday
: dates.add(date, val - weekday, DAY);
}
}
function monthMath(date, val){
var current = dates.month(date)
, newMonth = (current + val);
date = dates.month(date, newMonth)
if (newMonth < 0 ) newMonth = 12 + val
//month rollover
if ( dates.month(date) !== ( newMonth % 12))
date = dates.date(date, 0) //move to last of month
return date
}
function createAccessor(method){
return function(date, val){
if (val === undefined)
return date['get' + method]()
date = new Date(date)
date['set' + method](val)
return date
}
}
function createComparer(operator) {
return function (a, b, unit, maybeFoW) {
return operator(+dates.startOf(a, unit, maybeFoW), +dates.startOf(b, unit, maybeFoW))
};
}