10 examples of 'javascript is valid date' in JavaScript

Every line of 'javascript is valid date' 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
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}
1function isValidDate(value: number | string) {
2 return !Number.isNaN(+new Date(value));
3}
3function isValid(date){
4 if(date.length !== 6) return false;
5
6 var month = parseInt(date[4] + date[5]);
7 if(month > 12 || month === 0) return false;
8
9 var today = getToday();
10 if(parseInt(today) - parseInt(date) < 0) return false;
11
12 return !isNaN(parseInt(date));
13}
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}
1export function isValidDate(year: number, month: number, date?: number): boolean {
2 const d = new Date(year, month, date);
3
4 const dateValid = d.getDate() == date;
5 const monthValid = d.getMonth() == month;
6 const yearValid = d.getFullYear() == year;
7 const valid = dateValid && monthValid && yearValid;
8
9 return valid;
10}
24public static isValidDate(year: number, month: number, day: number) {
25 return (
26 month > 0 && month < 13 &&
27 year && year.toString().length === 4 &&
28 day > 0 &&
29 // Is it a valid day of the month?
30 day <= (new Date(year, month, 0)).getDate()
31 );
32}
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 }
1function checkdate (m, d, y) {
2 // discuss at: http://phpjs.org/functions/checkdate/
3 // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
4 // improved by: Pyerre
5 // improved by: Theriault
6 // example 1: checkdate(12, 31, 2000);
7 // returns 1: true
8 // example 2: checkdate(2, 29, 2001);
9 // returns 2: false
10 // example 3: checkdate(3, 31, 2008);
11 // returns 3: true
12 // example 4: checkdate(1, 390, 2000);
13 // returns 4: false
14
15 return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0))
16 .getDate()
17}
115function validTime (dt) {
116 if (dt && typeof dt === 'string') {
117 var r = reTime.exec(dt)
118 if (r) {
119 var h = parseInt(r[1])
120 var m = parseInt(r[2])
121 return h >= 0 && h <= 23 && m >= 0 && m <= 59
122 }
123 }
124 return false
125}
49function isDate(value) {
50 return /\d{2,4}-\d{2}-\d{2}[T -_]\d{2}:\d{2}:\d{2}/.test(value);
51}

Related snippets