Use PHP functions in JavaScript

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: 911.2217
    // 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();
}
external links: original PHP docs | raw js source

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.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Jorge Vazquez
23 Jun '09 Permalink

q  Disregard my last comment. After extensive testing, I realized this script was working fine. The problem came because I was using jscalendar (http://www.dynarch.com/projects/calendar/old/), a useful calendar wich I learned redefines the setFullYear function...

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

Gravatar
Brett Zamir
23 Jun '09 Permalink

q   @Jorge Vazquez: When I check with the following:

1
checkdate(3, 4, 2009);


and add an alert to verify the date that has been created:

1
2
3
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).

Gravatar
Jorge Vazquez
22 Jun '09 Permalink

q   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:

1
2
3
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.

Gravatar
Kevin van Zonneveld
27 Aug '08 Permalink

q  @ Pyerre: Thank you :) I've updated the function and also added your example in as a unit test so this can never happen again.

Gravatar
Pyerre
14 Aug '08 Permalink

q  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

Gravatar
Kevin van Zonneveld
3 Apr '08 Permalink

q   @ 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:

1
return ( myDate.getMonth() != month );


instead of this
1
return ( myDate.getMonth() == month );


So that made testing unreliable. Thanks for persisting in this matter! The function is fixed now.

Gravatar
Dominique
3 Apr '08 Permalink

q   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

1
myDate.getYear()

instead of
1
myDate.getFullYear()

Which explains the strange value given for the year...
But with respect to
1
2
myDate.setFullYear( 2008, 03, 31);
alert(myDate.getFullYear()+ &quot; &quot; + myDate.getMonth() + &quot; &quot; +myDate.getDate());

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.

Gravatar
Kevin van Zonneveld
2 Apr '08 Permalink

q  @ Dominique: Hi, I've added your example date and tried to reproduce it here: http://kevin.vanzonneveld.net/pj_tester.php

But the checkdate examples all give the expected output here. Is this the same for you?

Gravatar
Przemek
31 Mar '08 Permalink

q   (" - double quote) also. It gives &quot;. But you can fix it yourself by just moving

1
2
3
4
56
7
if (useQuoteStyle != 'ENT_NOQUOTES') {
        entities['34'] = '&quot;';
    }
 
    if (useQuoteStyle == 'ENT_QUOTES') {        entities['39'] = '&#039;';
    }


at the end of the table in get_html_translation_table


Contribute a New function

More functions

In this category

» checkdate
date
date_default_timezone_get
date_default_timezone_set
date_parse
getdate
gettimeofday
gmdate
gmmktime
gmstrftime
idate
localtime
microtime
mktime
strftime
strtotime
time
timezone_abbreviations_list
timezone_identifiers_list

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !