10 examples of 'future date validation in javascript' in JavaScript

Every line of 'future date validation in javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
87function isDateValid(value) {
88 const matched = value.match(dateRe);
89
90 if (matched) {
91 const epoch = Date.parse(value);
92 if (!epoch && epoch !== 0) return false;
93
94 const d = new Date(epoch);
95 d.setTime(d.getTime() + d.getTimezoneOffset() * 60 * 1000);
96
97 if (
98 d.getFullYear() == matched[1] &&
99 d.getMonth() + 1 == +matched[2] &&
100 d.getDate() == +matched[3]
101 ) {
102 return true;
103 }
104 }
105
106 return false;
107}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

2156getValidatedDate: function getValidatedDate(value) {
2157 var owner = this,
2158 result = '';
2159
2160 value = value.replace(/[^\d]/g, '');
2161
2162 owner.blocks.forEach(function (length, index) {
2163 if (value.length > 0) {
2164 var sub = value.slice(0, length),
2165 sub0 = sub.slice(0, 1),
2166 rest = value.slice(length);
2167
2168 switch (owner.datePattern[index]) {
2169 case 'd':
2170 if (sub === '00') {
2171 sub = '01';
2172 } else if (parseInt(sub0, 10) > 3) {
2173 sub = '0' + sub0;
2174 } else if (parseInt(sub, 10) > 31) {
2175 sub = '31';
2176 }
2177
2178 break;
2179
2180 case 'm':
2181 if (sub === '00') {
2182 sub = '01';
2183 } else if (parseInt(sub0, 10) > 1) {
2184 sub = '0' + sub0;
2185 } else if (parseInt(sub, 10) > 12) {
2186 sub = '12';
2187 }
2188
2189 break;
2190 }
2191
2192 result += sub;
2193
2194 // update remaining string
2195 value = rest;
2196 }
2197 });
2198
2199 return this.getFixedDateString(result);
2200 },
42function validateDate(value, meta) {
43 if (value && !utc(value).isValid())
44 return {[meta._id]: `${meta.label} must be a valid date`}
45}
30function parseDate(value) {
31 if (value instanceof Date) {
32 return value;
33 }
34 let date = new Date(value);
35 if (date.toString() === 'Invalid Date') {
36 throw new Error();
37 }
38 return date;
39}
15export function getValidDate(date: unknown): Date {
16 if (typeof date === 'undefined') {
17 throw new Error('expects a date');
18 }
19
20 date = date || new Date();
21 if (date instanceof Date) {
22 return date;
23 }
24
25 if (typeof date === 'number') {
26 if (isValidDate(date)) date = new Date(date);
27 }
28
29 if (typeof date === 'string') {
30 if (!isValidDate(date)) {
31 date = date.replace(/-/g, '/');
32 }
33
34 if (isValidDate(date as string)) {
35 date = new Date(date as string);
36 }
37 }
38
39 return date as Date;
40}
9export default function dateValidation(dateToValidate, options) {
10 return moment(dateToValidate, options).isValid();
11}
1function isValidDate(value: number | string) {
2 return !Number.isNaN(+new Date(value));
3}
20export default function parseDate(
21 date: string | number | Date,
22 mask = 'default',
23 locale = zhCN
24): Date {
25 if (date instanceof Date) {
26 return date;
27 }
28
29 if (typeof date === 'number') {
30 return new Date(date);
31 }
32
33 mask = mask || 'default';
34
35 const ret = parse(date, mask, locale);
36
37 if (!ret) {
38 return null;
39 }
40
41 return ret;
42}
106function dateHandler(date) {
107 var
108 dArray, // 数组化后的日期
109 ret;
110
111 if (typeof date === 'string') { // 如果date参数是string类型
112 if (rNumberstring.test(date)) { // 如果date参数是number string类型
113 ret = new Date(date);
114 } else { // 这里重新格式化,一般都是从服务端过来的数据,必须有年月日,并且顺序是年月日时分秒毫秒,并且7个值之间有间隔符,间隔符为\D
115 dArray = date.match(rDatestring); // 从string中提取new Date需要的参数
116 if(dArray.length < 3) { // 服务端时间戳,例如NET "/Date(562941040500+0800)/"
117 ret = new Date(+dArray[0]);
118 } else { // 格式化过的
119 ret = new Date(dArray[0], dArray[1] - 1, dArray[2] || 1, dArray[3] || 0, dArray[4] || 0, dArray[5] || 0, dArray[6] || 0);
120 }
121 }
122 } else if (typeof date === 'number' || Object.prototype.toString.call(date) === '[object Date]') { // 如果date参数是number类型、date类型
123 ret = new Date(+date);
124 } else if (date == null) { // 如果不存在date参数
125 ret = new Date();
126 } else {
127 return false;
128 }
129
130 return ret;
131 }
54function ensureDate (value) {
55 logger(`ensureDate`, value)
56 return isDate(value) ? value : new Date()
57}

Related snippets