-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser-date.js
More file actions
255 lines (217 loc) · 4.85 KB
/
parser-date.js
File metadata and controls
255 lines (217 loc) · 4.85 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
module.exports = ParserDate
/**
* Time constants
*/
var _second = 1000
var _minute = 60 * _second
var _hour = 60 * _minute
var _day = 24 * _hour
var _week = 7 * _day
//var _year = 56 * _week
//var _daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
/**
* Initialize `ParserDate`
*
* @param {Date} offset (optional)
* @return {Date|ParserDate}
* @api public
*/
function ParserDate (offset) {
if (!(this instanceof ParserDate)) return new ParserDate(offset)
this._changed = {}
this.date = new Date(offset)
}
/**
* Clone the current date
*/
ParserDate.prototype = {
clone: function () {
return new Date(this.date)
},
/**
* Has changed
*
* @param {String} str
* @return {Boolean}
*/
changed: function (str) {
if (this._changed[str] === undefined) return false
return this._changed[str]
},
/**
* add or subtract seconds
*
* @param {Number} n
* @return {ParserDate}
*/
second: function (n) {
var seconds = +n * _second
this.update(seconds)
this._changed['seconds'] = true
return this
},
/**
* add or subtract minutes
*
* @param {Number} n
* @return {ParserDate}
*/
minute: function (n) {
var minutes = +n * _minute
this.update(minutes)
this._changed['minutes'] = true
return this
},
/**
* add or subtract hours
*
* @param {Number} n
* @return {ParserDate}
*/
hour: function (n) {
var hours = +n * _hour
this.update(hours)
this._changed['hours'] = true
return this
},
/**
* add or subtract days
*
* @param {Number} n
* @return {ParserDate}
*/
day: function (n) {
var days = +n * _day
this.update(days)
this._changed['days'] = true
return this
},
/**
* add or subtract weeks
*
* @param {Number} n
* @return {ParserDate}
*/
week: function (n) {
var weeks = +n * _week
this.update(weeks)
this._changed['weeks'] = true
return this
},
/**
* add or subtract months
*
* @param {Number} n
* @return {ParserDate}
*/
month: function (n) {
var d = this.date
var day = d.getDate()
d.setDate(1)
var month = +n + d.getMonth()
d.setMonth(month)
// Handle dates with less days
var dim = this.daysInMonth(month)
d.setDate(Math.min(dim, day))
return this
},
/**
* get the days in the month
* todo: check it!
*/
daysInMonth: function (m) {
//var dim = _daysInMonth[m]
var leap = leapyear(this.date.getFullYear())
return (1 == m && leap) ? 29 : 28
},
/**
* add or subtract years
*
* @param {Number} n
* @return {ParserDate}
*/
year: function (n) {
var yr = this.date.getFullYear()
yr += +n
this.date.setFullYear(yr)
this._changed['years'] = true
return this
},
/**
* Set the time
*
* @param {Number} h
* @param {Number} m
* @param {Number} s
* @return {ParserDate}
*
* todo: meridiem?
*/
time: function (h, m, s/*, meridiem*/) {
if (h === false) {
h = this.date.getHours()
} else {
h = +h || 0
this._changed['hours'] = h
}
if (m === false) {
m = this.date.getMinutes()
} else {
m = +m || 0
this._changed['minutes'] = m
}
if (s === false) {
s = this.date.getSeconds()
} else {
s = +s || 0
this._changed['seconds'] = s
}
this.date.setHours(h, m, s)
return this
},
/**
* go to day of week
*
* @param {Number} d
* @param {Number} n
* @return {ParserDate}
*/
updateDay: function (d, n) {
n = +(n || 1)
var diff = (d - this.date.getDay() + 7) % 7
if (n > 0) --n
diff += (7 * n)
this.update(diff * _day)
return this
},
/**
* Update the date
*
* @param {Number} ms
* @return {ParserDate}
* @api private
*/
update: function (ms) {
this.date = new Date(this.date.getTime() + ms)
return this
}
}
/**
* Dynamically create day functions (sunday(n), monday(n), etc.)
*/
var days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
days.forEach(function (day, i) {
ParserDate['prototype'][days[i]] = function (n) {
this._changed['days'] = true
this.updateDay(i, n)
}
})
/**
* leap year
*
* @param {Number} yr
* @return {Boolean}
*/
function leapyear (yr) {
return (yr % 4 === 0 && yr % 100 !== 0) || yr % 400 === 0
}