0

I want to validate dates by Javascript and found this nice answer:

https://stackoverflow.com/a/1353711/3391783

but when i try to use it to validate dates, it seems like Javascript is auto-correcting my date by taking the closest valid date. so this will return true even though 2014-11-31 is not a valid date (Javascript months start at 0, so 10 equals November):

    function isValidDate(d) {
      if ( Object.prototype.toString.call(d) !== "[object Date]" )
        return false;
      return !isNaN(d.getTime());
    }
    
    var test_date = new Date(2014, 10, 31);
    
    console.log( test_date );
    console.log( isValidDate(test_date) );

seems like creating the Date is automatically switching it to 2014-12-01 which is a correct date. but I would like to be able to validate user input without changing it.

So how can i create an invalid new Date() in Javascript?
Or is there a much simpler way to do this?

3 Answers 3

3

You can use the auto-correction in the Date object to validate the date. Just check the input against what you have in the Date object:

var y = 2014, m = 10, d = 31;

var test_date = new Date(y, m, d);

var valid =
    test_date.getFullYear() == y &&
    test_date.getMonth() == m &&
    test_date.getDate() == d;

document.write(valid);

Sign up to request clarification or add additional context in comments.

1 Comment

oh, thanks. that's actually a cool solution. "if javascript decides to change my date - it has to be wrong" :)
2

When it comes to handling dates in JavaScript, I'm a big fan of Moment.js. As you can see here, they do a good job of validating dates: http://momentjs.com/docs/#/parsing/is-valid/

new Date(2013, 25, 14).toString(); // "Sat Feb 14 2015 00:00:00 GMT-0500 (EST)"
moment([2015, 25, 35]).format();   // 'Invalid date'

1 Comment

thanks. will try this library, but i kinda would like to restrict my work to jQuery and pure Javascript.
0

Here's a function I wrote a while back that demonstrates Guffa's solution.

    function isValidDate(checkDate) {
    if(!/\d\d\/\d\d\/\d\d\d\d/.test(checkDate)) {
            return false; // checkDate is not formatted as ##/##/####
        } else {
            // split checkDate into three pieces
            var strMM = checkDate.split('/')[0];
            var strDD = checkDate.split('/')[1];
            var strYYYY = checkDate.split('/')[2];

            // create new Date() object from split pieces
            var strDateCheck = new Date(strYYYY,(strMM - 1),strDD); 

            // evaluate each piece of resulting date object against each corresponding piece of checkDate
            if(((strDateCheck.getMonth() + 1) == strMM) && (strDateCheck.getDate() == strDD) && (strDateCheck.getFullYear() == strYYYY)) {
                /* if you wish, add additional validation constraints here */
                return true; // all three pieces match exactly
            } else {
                return false; // at least one piece did not match
            }
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.