how to validate a date in javascript

Not many people are aware that the date object in javascript has intrinsic knowledge of dates. It’s a misconception that date validation has to be a lenghty process.

Let’s consider the regular Y,M,D format, where a date is set using

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

Now we can take advantage of the fact that javascript will automatically rewrite a date to it’s nearest valid neighbour. Take e.g. Dec 32nd, 2006. Of course this date does not exist, and javascript knows this! That’s why it is automatically converted to Jan 1st, 2007. With this knowledge we can conclude that any valid date will not be altered, and any invalid date will be altered by javascript. Thus to test for a valid date all we need to test is if the date parameter values y,m and d have changed or not.

Here’s how:

function isDate(y,m,d)
{
var date = new Date(y,m-1,d);
var convertedDate =
""+date.getFullYear() + (date.getMonth()+1) + date.getDate();
var givenDate = "" + y + m + d;
return ( givenDate == convertedDate);
}

Now to test it, here’s an example, checking Jan 40th, 2007.

window.alert (isDate(2007,1,40));

This will show ‘false’.

If that doesn’t convince you, try

window.alert (isDate(2007,2,29));

How about that?

Advertisements

14 comments so far

  1. Steve on

    Thanks for this post. I was looking all over the web getting really disgusted that there wasn’t a straight forward way to solve this problem without hand writing a slew of complicated code to do a task that should just be built into the language.

    I like your solution because it is straight forward and therefore less prone to error. Thank You!

  2. michiel on

    Glad to help!

  3. Eric Hahn on

    HI Michiel,

    would it be possible to alter what you have above to test that a user choose a particular ‘day’- date and date format are unimportant in that they are forced to choose from a pop up calendar- but i can’t seem to make them choose a sunday start date via the calender widgit, so i thought i’d just validate that they choose a sunday…

    any ideas?

    Thanks!!
    eric

  4. js on

    Thanks for taking the time – very helpful.

  5. Mark Skayff on

    Hey thanks. It looks clean and smooth. Good one there!

  6. leo on

    Thank you for your answer. Actually you can remove my last comment, there’s a bug in my code… that’s why it didn’t worked for leap years, sorry for that 😦

  7. Durval Henke on

    is not valid for 10/22/1950 or 1950/10/22

    • michiel on

      It is for me. isDate(1950,10,22) results in true…. Maybe you are not using the function correctly.

  8. Ed S on

    Your example returns false for me in IE8

    • michiel on

      Thank you, then it works, because January 40th should return false. It doesn’t exist, right?

  9. akem on

    thanks a lot, very accurate solution

  10. Dineas on

    I need a javascript codes for checking the dates whether the date is available or not because im doing a clinic system for my project.There is an appointment booking part i need to do so, i need the javascript codes to check the dates while patients are booking appointment with the doctor.Thanks in advance.Its urgent.

    • michiel on

      Sorry, I haven’t checked my mail in a long time. You are free to use my code here, but for what you need I think you need more than just check if a date is valid. You need a database and check the date exists already in the system.

  11. AntonMMF on

    Need add check on leading zero

    var y = parseInt(y,10);
    var m = parseInt(m,10);
    var d = parseInt(d,10);


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: