JavaScript checkdate
Returns true(1) if it is a valid date in gregorian calendar
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 | function checkdate ( m, d, y ) { // Returns true(1) if it is a valid date in gregorian calendar // // version: 1008.1718 // discuss at: http://phpjs.org/functions/checkdate // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Pyerre // + improved by: Theriault // * example 1: checkdate(12, 31, 2000); // * returns 1: true // * example 2: checkdate(2, 29, 2001); // * returns 2: false // * example 3: checkdate(03, 31, 2008); // * returns 3: true // * example 4: checkdate(1, 390, 2000); // * returns 4: false return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate(); } |
Examples
» Example 1
Running
1 | checkdate(12, 31, 2000); |
Should return
1 | true |
» Example 2
Running
1 | checkdate(2, 29, 2001); |
Should return
1 | false |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have checkdate goodness in JavaScript.
@Jorge Vazquez: When I check with the following:
checkdate(3, 4, 2009);
and add an alert to verify the date that has been created:
var myDate = new Date();
myDate.setFullYear( year, (month - 1), day );
alert(myDate)
it works correctly in Firefox, IE, Safari, Opera in showing March 4 as the date...
What test data are you using? Please show the exact code so we can identify the problem...
By the way, I added a few more checks to fit the checkdate() behavior (ensuring the month was between 1 and 12 and the year between 1 and 32767).
I've found an error with the code: myDate.setFullYear( year, (month - 1), day ); would only set the year, but not the month nor the day.
I passed the parameters correctly, however only the year part works. I even included leading zeroes to the months after the sustraction. Ultimately I solved it setting the date when you create the variable:
newmonth = (month - 1); var myDate = new Date(year, newmonth , day); myDate.setFullYear(year, newmonth , day);
I dont know why, maybe it was an error in my PC. However I tried it in many different browsers and it work in any of them.
@ Pyerre: Thank you :) I've updated the function and also added your example in as a unit test so this can never happen again.
i found a bug
checkdate(1,390,2000) returns true
because setFullYear will add the 390 days to the date but the month will be the same
you can make return ((myDate.getMonth()+1) == month && day<32);
good trip in Amsterdam
@ Dominique: You're right it does work with ranges from 0-11. Confusingly enough, getMonth also works with ranges from 0-11, so that compensated somewhat for the behaviour. But a third bug was this:
[CODE="Javascript"]
return ( myDate.getMonth() != month );
[/CODE]
instead of this
[CODE="Javascript"]
return ( myDate.getMonth() == month );
[/CODE]
So that made testing unreliable. Thanks for persisting in this matter! The function is fixed now.
Hello Kevin, and thank you for your answer.
Part of my comment was mistaken, but part of it still "sticks".
In my test, I used
[CODE="Javascript"]
myDate.getYear()
[/CODE]
instead of
[CODE="Javascript"]
myDate.getFullYear()
[/CODE]
Which explains the strange value given for the year...
But with respect to
[CODE="Javascript"]
myDate.setFullYear( 2008, 03, 31);
alert(myDate.getFullYear()+ " " + myDate.getMonth() + " " +myDate.getDate());
[/CODE]
giving 2008 04 01 as an answer, I persist: I definitely get that result.
Reading the Javascript specifications it is not surprising since it states that "setFullYear" takes a month parameter going from 0 to 11, so it considers month 03 to be april (which has 30 days) and hence the 31st of April to be actually the 1st of May (month 04)...
So, if the code you post is the actual checkdate code, I do not understand how it could give the right answer... I am puzzled.


Jorge Vazquez
23 Jun '09
Of course it took me a while to find this out. So, great script, and I'm sorry, my bad!