Javascript date allows invalid data (e.g. February 30th)

How to confirm the date? I do not mean the format, but the logic. For example: February 30, invalid date.

var date = new Date("2015-02-29T13:02:49.073Z"); // 2015 Feb 29th does not exist console.log(date.toISOString()); 

Returns 2015-03-01T13: 02: 49.073Z (March 1).

But I need information that this date (input) is not valid.

Edit: Tested in Chrome. Firefox returns an "invalid date". But not to parse. Only when a date is used (e.g. toISOString ()) is an exception thrown.

 try { var date = new Date("2015-02-29T13:02:49.073Z"); console.log(date.toISOString()); } catch(e) { console.log("error: " + e.message); } 

Firefox:

wrong date

Chrome:

(nothing, just switched to the next date.)

Summary: browser dependent. Therefore, it is not recommended to use.

Jsfiddle example

+5
source share
6 answers

The easiest thing I can think of is to convert the processed date to an ISO string and compare it with the original input:

 var input = "2015-02-29T13:02:49.073Z" var date = new Date(input); var isValid = (input === date.toISOString()); 
+4
source

I use this function to check if the date is valid or not:

 function isValidDate(year, month, day) { month = month - 1; var d = new Date(year, month, day); if (d.getFullYear() == year && d.getMonth() == month && d.getDate() == day) { return true; } return false; } 
+4
source

I wrote a little function for you:

 function check_date(str){ try{ return str == new Date(str).toISOString() }catch(e){ return false; } } 

try it

 console.log(check_date('2015-02-01T13:02:49.073Z')); console.log(check_date('2015-02-35T13:02:49.073Z')); console.log(check_date('2015-02-29T13:02:49.073Z')); 

https://jsfiddle.net/8o040ctr/

+2
source

I found this behavior pretty funny. I simply converted the input to a date (as you did), and then updated the text in the text box to show the date interpreted by JavaScript so that the user sees the final (valid) date.

Fortunately, we can do much better now. Moment.js is a fantastic library for handling date / time values ​​in JavaScript. It certainly made life a lot easier for me!

http://momentjs.com/

0
source

Dates in browsers are always very complicated, I suggest you use js lib as a moment: http://momentjs.com/

then you can use the .isValid () method for ex .:

 moment('03:55', 'HH:mm').isValid(); moment('2012-05-25', 'YYYY-MM-DD').isValid(); moment('2015-02-29T13:02:49.073Z', "YYYY-MM-DDTHH:mm:ss", true).isValid(); 
0
source

If you pass a line starting with the format "YYYY-MM", as in the question, you can use this function:

 function makeDate(s) { var date = new Date(s); if (date.toISOString(date).substr(0,7) != s.substr(0,7)) throw "Invalid date"; return date; } dt = makeDate('2015-02-29T13:02:49.073Z'); 

This makeDate function will replace the usual call to new Date(...) , but it will also throw an error in Chrome when an invalid date string is passed to it.

0
source

Source: https://habr.com/ru/post/1245626/


All Articles