Pretty simple question, how do i validate a datetime, so the input is both the correct format, but also a valid date unlike 2015-02-30 ...
.
2015-06-28 16:06:35 //Valid
Pretty simple question, how do i validate a datetime, so the input is both the correct format, but also a valid date unlike 2015-02-30 ...
.
2015-06-28 16:06:35 //Valid
Try using moment.js. It's quite good at taking an input of something date-like and parsing it into something usable. It also has an isValid
method for determining if the lib was able to parse the date input it was given.
http://momentjs.com/docs/#/parsing/is-valid/
var feb30 = moment('2015-02-30');
var jun28 = moment('2015-06-28');
str = "Feb 30: " + feb30.isValid(); // false
str += "\nJun 28: " + jun28.isValid(); // true
alert(str);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment-with-locales.min.js"></script>
If you do var d = new Date('2015-02-30')
, it will roll over and return the date of 2015-03-02
. So, if you compare strings of these values they will be unequal.