-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.17 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
'use strict';
module.exports = function(date, format) {
format = (format || 'MDY').toLowerCase().split('');
var inputs = date.split(' ');
var dateComponents = inputs[0].split('/');
var day;
var month;
var year;
var hours;
var minutes;
var seconds;
var timeComponents = inputs[1] && inputs[1].split(':');
dateComponents.forEach(function(component, index) {
switch(format[index]) {
case 'd':
day = parseInt(component, 10);
break;
case 'm':
month = parseInt(component - 1, 10);
break;
case 'y':
year = parseInt(component, 10);
break;
}
});
if (timeComponents && timeComponents.length) {
hours = timeComponents[0];
minutes = timeComponents[1];
seconds = timeComponents[2];
}
var targetDate = new Date(year, month, day);
if (hours !== undefined) targetDate.setHours(hours);
if (minutes !== undefined) targetDate.setMinutes(minutes);
if (seconds !== undefined) targetDate.setSeconds(seconds);
var now = Date.now();
return now > targetDate;
};