-
Notifications
You must be signed in to change notification settings - Fork 1
/
dateCheck.js
102 lines (71 loc) · 2.61 KB
/
dateCheck.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
// date format: YYYY-MM-DD
// Functions to assign year / month / day to consts & convert strings to integers
const getYear = (dateString) => parseInt(dateString.slice(0,4));
const getMonth = (dateString) => parseInt(dateString.slice(5,7));
const getDay = (dateString) => parseInt(dateString.slice(8,10));
// One of these values is returned when the checkDates() function is called
const dateApproved = true;
const dateRejected = false;
// checkDates() function determines if the provided arrival date is at least one day earlier than the provided departure date that the user has entered
// if dates are valid returns 'true', if dates are invalid returns false
function checkDates(arrivalDate,departureDate) {
// assigning the dates to objects allows us to easily access the year / month / day for arrival and departure dates
const arrival = {
year: getYear(arrivalDate),
month: getMonth(arrivalDate),
day: getDay(arrivalDate)
};
const departure = {
year: getYear(departureDate),
month: getMonth(departureDate),
day: getDay(departureDate)
}
if (arrival.year == departure.year) {
if ( ((arrival.month == departure.month) && (arrival.day < departure.day)) || arrival.month < departure.month ) {
return dateApproved;
} else {
return dateRejected;
}
} else if (arrival.year < departure.year) {
return dateApproved;
} else {
return dateRejected;
}
}
// // Check blackout dates - incomplete for now
// const blackedOut = [
// {
// // room: 'irrigation',
// blackOutStart: '2019-02-01',
// blackOutEnd: '2019-02-05'
// },
// {
// // room: 'irrigation',
// blackOutStart: '2019-03-01',
// blackOutEnd: '2019-03-05'
// }
// ];
// function checkBlackOutDates() {
// blackedOut.forEach( (dates) => {
// // set blackout start date (int's) to an object
// let start = {
// year: getYear(dates.blackOutStart),
// month: getMonth(dates.blackOutStart),
// day: getDay(dates.blackOutStart)
// };
// // set blackout end date (int's) to an object
// let end = {
// year: getYear(dates.blackOutEnd),
// month: getMonth(dates.blackOutEnd),
// day: getDay(dates.blackOutEnd)
// };
// //if arrival date OR the departure date lies between the blackout start and end dates, throw and error
// //e.g. 2019/2019 or 2019/2020
// //is the arrival between the dates?
// if ( (arrival.year >= start.year && arrival.year <= end.year ) ) { //year check
// } else {
// //dates blacked not out
// }
// });
// }
module.exports = {checkDates};