-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
132 lines (99 loc) · 2.36 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
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
const moment = require("moment");
module.exports = {
/**
* Validate input
* Validation with respect to date, month and year
* Validation for valid month
* Validation for valid day
* @param date
* @param dateFormat
* @returns {*}
*/
isValidDate: function ( date, dateFormat ) {
const formattedDate = this.dateFormatting( date, dateFormat );
date = formattedDate.split( "/" );
if ( date.length !== 3 ) {
return false;
}
if ( isNaN( date[0] ) || isNaN( date[1] || isNaN( date[2] ) ) ) {
return false;
}
if ( !this.isValidDD ( date[0] ) ) {
return false;
}
if ( !this.isValidMM ( date[1] ) ) {
return false;
}
if ( !this.isValidYYYY ( date[2] ) ) {
return false;
}
return this.isValidDDMMYYYY( date[0], date[1], date[3] );
},
/**
* Formatting date to custom format (DD/MM/YYYY)
* @param date
* @param currentFormat
* @returns {string}
*/
dateFormatting: function ( date, currentFormat ) {
return moment( date, currentFormat ).format( "DD/MM/YYYY" );
},
isValidDD: function ( dd ) {
return !( dd.length > 2 || Number( dd ) > 31 );
},
isValidMM: function ( mm ) {
return !( mm.length > 2 || Number( mm ) > 12 );
},
isValidYYYY: function ( yyyy ) {
return yyyy.length === 4;
},
isValidDDMMYYYY: function ( dd, mm, yyyy ) {
dd = Number( dd );
mm = Number( mm );
yyyy = Number( yyyy );
const isLeapYear = ( yyyy % 4 === 0 );
switch ( mm ) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if ( dd > 31 ) {
return false;
}
break;
case 2:
if ( isLeapYear && dd > 29 ) {
return false;
}
if ( !isLeapYear && dd > 28 ) {
return false;
}
break;
case 4:
case 6:
case 9:
case 11:
if ( dd > 30 ) {
return false;
}
break;
default:
return false;
break;
}
return true;
},
/**
* Check if given date is past the current date
* @param date
* @param currentFormat
* @returns {boolean}
*/
isDatePast: function ( date, currentFormat ) {
const currentDate = moment().format(currentFormat);
return moment(date).isBefore(currentDate);
}
};