-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidateDate.js
More file actions
166 lines (111 loc) · 4.06 KB
/
validateDate.js
File metadata and controls
166 lines (111 loc) · 4.06 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
function getDateStatus(date) {
date = date.trim();
let monthLen = 0;
const monthsRus = [
"января", "февраля", "марта", "апреля", "мая", "июня",
"июля", "августа", "сентября", "октября", "ноября", "декабря"
];
const monthsEngFull = [
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];
const monthsEngShort = [
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
];
const monthLens = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
function isInt(val) {
return !isNaN(val) && Number.isInteger(+val);
}
function validDay(day, monthLen) {
return isInt(day) && +day >= 1 && +day <= monthLen;
}
function validMonth(month) {
return isInt(month) && +month >= 1 && +month <= 12;
}
function validYear(year) {
return isInt(year) && +year >= 100;
}
function leapYear(year) {
let leap = false;
year = +year;
if (year % 4 === 0) {
if (year % 100 === 0) {
if (year % 400 === 0) {
leap = true;
}
}
else {
leap = true;
}
}
return leap
}
function getMonthLen(m, y, monthList=[]) {
let monthLen = 0;
if (monthList.length !== 0) {
m = monthList.indexOf(m) + 1;
}
if (validMonth(m)) {
monthLen = monthLens[+m - 1];
if (validYear(y) && leapYear(y) && +m === 2) {
monthLen = 29
}
}
return monthLen;
}
function checkDMY(str, sep) {
const parts = str.split(sep);
if (parts.length !== 3) return false;
let [d, m, y] = parts.map(p => p.trim());
monthLen = getMonthLen(m, y);
return validDay(d, monthLen) && validMonth(m) && validYear(y);
}
function checkYMD(str, sep) {
const parts = str.split(sep);
if (parts.length !== 3) return false;
let [y, m, d] = parts.map(p => p.trim());
monthLen = getMonthLen(m, y);
return validYear(y) && validMonth(m) && validDay(d, monthLen);
}
function checkDayMonthRusYear(str) {
const parts = str.split(" ");
if (parts.length !== 3) return false;
let [d, m, y] = parts;
monthLen = getMonthLen(m, y, monthsRus);
return validDay(d, monthLen) && monthsRus.includes(m) && validYear(y);
}
function checkMonthEngDayYear(str, monthList) {
const parts = str.split(",").map(p => p.trim());
if (parts.length !== 2) return false;
const [monthDay, y] = parts;
const sub = monthDay.split(" ");
if (sub.length !== 2) return false;
const [m, d] = sub;
monthLen = getMonthLen(m, y, monthList);
return monthList.includes(m) && validDay(d, monthLen) && validYear(y);
}
function checkYearMonthEngDay(str, monthList) {
const parts = str.split(",").map(p => p.trim());
if (parts.length !== 2) return false;
const [y, monthDay] = parts;
const sub = monthDay.split(" ");
if (sub.length !== 2) return false;
const [m, d] = sub;
monthLen = getMonthLen(m, y, monthList);
return validYear(y) && monthList.includes(m) && validDay(d, monthLen);
}
return [
checkDMY(date, "."), //1
checkDMY(date, "/"), //2
checkDMY(date, "-"), //3
checkYMD(date, "."), //4
checkYMD(date, "/"), //5
checkYMD(date, "-"), //6
checkDayMonthRusYear(date), //7
checkMonthEngDayYear(date, monthsEngFull), //8
checkMonthEngDayYear(date, monthsEngShort),//9
checkYearMonthEngDay(date, monthsEngFull), //10
checkYearMonthEngDay(date, monthsEngShort) //11
].map(x => x ? 1 : 0);
}