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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435 |
1×
1×
1×
1×
1×
1×
1×
1×
85×
27×
85×
5×
80×
78×
2×
1×
2×
1×
2×
1×
1×
1×
7×
7×
1×
3×
3×
1×
2×
1×
5×
1×
8×
1×
6×
1×
5×
1×
3×
1×
2×
1×
2×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
2×
1×
2×
1×
2×
1×
2×
1×
2×
1×
2×
1×
1×
1×
2×
1×
1×
1×
3×
1×
2×
1×
1×
1×
1×
1×
1×
1×
2×
1×
2×
1×
3×
1×
3×
1×
3×
1×
2×
1×
| 'use strict'
let moment
try {
moment = require('moment')
} catch (e) {
moment = window.moment
}
const DAY_TOKEN = 'd'
const YEAR_TOKEN = 'y'
const MONTH_TOKEN = 'M'
const HOURS_TOKEN = 'h'
/**
* @module MomentAdapter
*/
/**
* Get an immutable moment date
* @param {String} date
* @param {Boolean} immutable
* @private
* @return {Moment}
*/
function _getMoment (date, immutable) {
if (immutable === undefined) {
immutable = true
}
if (moment.isMoment(date)) {
return immutable === true ? date.clone() : date
}
if (date instanceof Date) {
return moment(date)
}
return null
}
/**
* Get months, an array of string
* @return {Array<String>} List of the available months
*/
function months () {
return moment.months()
}
/**
* Get week days
* @return {Array<String>}
*/
function weekdays () {
return moment.weekdaysShort()
}
/**
* First day of week according to locale
* @return {Number}
*/
function firstDayOfWeek () {
return moment.localeData().firstDayOfWeek()
}
/**
* @param {Date} date
* @return {Boolean}
*/
function isValid (date) {
date = _getMoment(date, false)
return date instanceof moment && date.isValid()
}
/**
* @param {String} dateString
* @param {String} format
* @return {Date|Boolean} false if invalid or the parsed date, parsing is heaving let's do this only once
*/
function isValidWithFormat (dateString, format) {
const testDate = moment(dateString, format, true)
if (this.isValid(testDate) === true) {
return testDate.toDate()
}
return false
}
/**
* Get year
* @param {Date} date
* @return {Number}
*/
function getYear (date) {
return _getMoment(date, false).year()
}
/**
* Get Month
* @param {Date} date
* @return {Number}
*/
function getMonth (date) {
return _getMoment(date, false).month()
}
/**
* Get Date
* @param {Date} date
* @return {Number}
*/
function getDate (date) {
return _getMoment(date, false).date()
}
/**
* Get Hours
* @param {Date} date
* @return {Number}
*/
function getHours (date) {
return _getMoment(date, false).hours()
}
/**
* Get Minutes
* @param {Date} date
* @return {Number}
*/
function getMinutes (date) {
return _getMoment(date, false).minutes()
}
/**
* Get Seconds
* @param {Date} date
* @return {Number}
*/
function getSeconds (date) {
return _getMoment(date, false).seconds()
}
/**
* Get Milliseconds
* @param {Date} date
* @return {Number}
*/
function getMilliseconds (date) {
return _getMoment(date, false).milliseconds()
}
/**
* Set Date
* @param {Date} date
* @param {Number} day
* @return {Date}
*/
function setDate (date, day) {
return _getMoment(date).date(day).toDate()
}
/**
* Set Minutes
* @param {Date} date
* @param {Number} minutes
* @return {Date}
*/
function setMinutes (date, minutes) {
return _getMoment(date).minutes(minutes).toDate()
}
/**
* Set Hours
* @param {Date} date
* @param {Number} hours
* @return {Date}
*/
function setHours (date, hours) {
return _getMoment(date).hours(hours).toDate()
}
/**
* Set Month
* @param {Date} date
* @param {Number} month
* @return {Date}
*/
function setMonth (date, month) {
return _getMoment(date).month(month).toDate()
}
/**
* Set Year
* @param {Date} date
* @param {Number} year
* @return {Date}
*/
function setYear (date, year) {
return _getMoment(date).year(year).toDate()
}
/**
* Add Days
* @param {Date} date
* @param {Number} num days to add
* @return {Date}
*/
function addDays (date, num) {
return _getMoment(date).add(num, DAY_TOKEN).toDate()
}
/**
* Add Months
* @param {Date} date
* @param {Number} num months to add
* @return {Date}
*/
function addMonths (date, num) {
return _getMoment(date).add(num, MONTH_TOKEN).toDate()
}
/**
* Add Years
* @param {Date} date
* @param {Number} num years to add
* @return {Date}
*/
function addYears (date, year) {
return _getMoment(date).add(year, YEAR_TOKEN).toDate()
}
/**
* Add Hours
* @param {Date} date
* @param {Number} num hours to add
* @return {Date}
*/
function addHours (date, hours) {
return _getMoment(date).add(hours, HOURS_TOKEN).toDate()
}
/**
* Subtract days
* @param {Date} date
* @param {Number} num days to subtract
* @return {Date}
*/
function subDays (date, num) {
return _getMoment(date).subtract(num, DAY_TOKEN).toDate()
}
/**
* Subtract months
* @param {Date} date
* @param {Number} num months to subtract
* @return {Date}
*/
function subMonths (date, num) {
return _getMoment(date).subtract(num, MONTH_TOKEN).toDate()
}
/**
* Format a Date and return a string
* @param {Date} date
* @param {String} format
* @return {String}
*/
function format (date, format) {
return _getMoment(date, false).format(format)
}
/**
* Get the number of days in the current date month
* @param {Date} date
* @return {Number}
*/
function daysInMonth (date) {
return _getMoment(date, false).daysInMonth()
}
/**
* Get number of the day in the week (from 0 to 6) for the given month on the first day
* @param {Date} date
* @returns {Number}
*/
function firstWeekDay (date) {
return +_getMoment(date).date(1).format('e')
}
/**
* Reset a date seconds
* @param {Date} date
* @returns {Date}
*/
function resetSeconds (date) {
return _getMoment(date).seconds(0).milliseconds(0).toDate()
}
/**
* Reset a date minutes
* @param {Date} date
* @returns {Date}
*/
function resetMinutes (date) {
return _getMoment(this.resetSeconds(date)).minutes(0).toDate()
}
/**
* Reset a date hours
* @param {Date} date
* @returns {Date}
*/
function resetHours (date) {
return _getMoment(this.resetMinutes(date)).hours(0).toDate()
}
/**
* isBefore
* @param {Date} date
* @param {Date} comparison
* @return {Boolean}
*/
function isBefore (date, comparison) {
return _getMoment(date, false).isBefore(comparison)
}
/**
* isAfter
* @param {Date} date
* @param {Date} comparison
* @return {Boolean}
*/
function isAfter (date, comparison) {
return _getMoment(date, false).isAfter(comparison)
}
/**
* isSameOrAfter (comparison must be done on a DAY basis)
* @param {Date} date
* @param {Date} comparison
* @return {Boolean}
*/
function isSameOrAfter (date, comparison) {
return _getMoment(date, false).isSameOrAfter(comparison, DAY_TOKEN)
}
/**
* isSameOrBefore (comparison must be done on a DAY basis)
* @param {Date} date
* @param {Date} comparison
* @return {Boolean}
*/
function isSameOrBefore (date, comparison) {
return _getMoment(date, false).isSameOrBefore(comparison, DAY_TOKEN)
}
/**
* isSameDay
* @param {Date} date
* @param {Date} comparison
* @return {Boolean}
*/
function isSameDay (date, comparison) {
return _getMoment(date, false).isSame(comparison, DAY_TOKEN)
}
/**
* isSameHours
* @param {Date} date
* @param {Date} comparison
* @return {Boolean}
*/
function isSameHours (date, comparison) {
return _getMoment(date, false).isSame(comparison, HOURS_TOKEN)
}
/**
* isSameMonth
* @param {Date} date
* @param {Date} comparison
* @return {Boolean}
*/
function isSameMonth (date, comparison) {
return _getMoment(date, false).isSame(comparison, MONTH_TOKEN)
}
/**
* An uppercased meridiem (AM or PM)
* @param {Date} date
* @return {String}
*/
function getMeridiem (date) {
return _getMoment(date, false).format('A')
}
module.exports = {
_getMoment: _getMoment,
months: months,
weekdays: weekdays,
firstDayOfWeek: firstDayOfWeek,
isValid: isValid,
isValidWithFormat: isValidWithFormat,
getYear: getYear,
getHours: getHours,
getMonth: getMonth,
getDate: getDate,
getMinutes: getMinutes,
getSeconds: getSeconds,
getMilliseconds: getMilliseconds,
setDate: setDate,
setMinutes: setMinutes,
setHours: setHours,
setMonth: setMonth,
setYear: setYear,
addDays: addDays,
addMonths: addMonths,
addYears: addYears,
addHours: addHours,
subDays: subDays,
subMonths: subMonths,
format: format,
daysInMonth: daysInMonth,
firstWeekDay: firstWeekDay,
resetSeconds: resetSeconds,
resetMinutes: resetMinutes,
resetHours: resetHours,
isBefore: isBefore,
isAfter: isAfter,
isSameOrAfter: isSameOrAfter,
isSameOrBefore: isSameOrBefore,
isSameDay: isSameDay,
isSameHours: isSameHours,
isSameMonth: isSameMonth,
getMeridiem: getMeridiem,
}
|