10 examples of 'javascript invalid date' in JavaScript

Every line of 'javascript invalid 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
this disclaimer
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}
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

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 }
43function parseStartDate (date: ?string) {
44 if (!date) {
45 return undefined;
46 }
47 if (new Date(date).toString() === 'Invalid Date') {
48 return undefined;
49 }
50 return new Date(date);
51}
1function isValidDate(value: number | string) {
2 return !Number.isNaN(+new Date(value));
3}
567function parseDate(date) {
568 if (date) {
569 try { return new Date(Date.parse(date)); }
570 catch (e) { /* ignore invalid dates */ }
571 }
572}
46function parseDate(str) {
47 if (Y.Lang.isNull(str) || Y.Lang.isUndefined(str))
48 return null;
49 var parts = str.split(/[- :]/);
50 if ( parts.length < 3 )
51 return null;
52 while ( parts.length < 6 )
53 parts.push(0);
54 return new Date(parts[0], parts[1]-1, parts[2], parts[3], parts[4], parts[5]);
55}
46function parseDate(date) {
47 var re = date.match("([0-9]+)-([0-9]+)-([0-9]+) ([0-9]+):([0-9]+):.*");
48 return (re[2] + '/' + re[3] + '/' + re[1] + ' ' + re[4] + ':' + re[5]);
49}
120function parseDate (date) {
121 date = date.split('-')
122
123 var year = parseInt(date[0])
124 var month = parseInt(date[1]) - 1
125 var day = parseInt(date[2])
126
127 return new Date(year, month, day)
128}
24function parseDate(dateCode) {
25 const {y, m, d, H, M, S} = XLSX.SSF.parse_date_code(dateCode)
26
27 return new Date(`${y}/${m}/${d} ${H}:${M}:${S}`).getTime()
28}
105export function parseDate(d: string): Date {
106 const matches = d.match(/\d+/);
107 return new Date(parseInt(matches![0], 10));
108}

Related snippets