1. Home
  2. Computing & Technology
  3. JavaScript

Validating Dates

Join the Discussion

Questions? Comments?

Just about all the date manipulations that you will ever want to do are most easily done by creating a date object to hold the date and then performing all of the manipulations that you require using that date object. Of course in order to be able to create a date object that can be meaningfully used in the subsequent processing you first need to ensure that the date you set it to is in fact a valid date.

Any date that we get our visitors to enter will either be entered separately as a year, month, and day or can probably be split up into those component parts fairly easily. Assuming that we have the date as three separate fields like that then we can set up a simple function that will not only validate for us that those three fields represent a valid date but we can have the function create the date object for us while it is performing the validation (since the easiest way to validate the fields is to create a date object and there is no point in doing that twice).

So what sorts of validations do we need to perform on our year, month, and day in order to ensure that they represent a valid date? Well first of all they all need to be numbers and so testing all three as being numeric is the first step. Not just any numbers are valid either since the month needs to be a value between one and twelve and the day needs to be between one and however many days there are in the month.

After testing that all three values are numeric, the easiest way of testing if the month and day are within the appropriate ranges is to use the numbers supplied to create a date object. If the day is outside of the valid range for the month then the date object will be set to the date that is that number of days from the start of the specified month and the month (and possibly year) fields will be updated as required. Similarly if the month is outside of the valid range then the year will be updated appropriately. We can therefore easily test if the values supplied are valid by verifying that the year, month, and day parts of the date object that we created are the same values as those that we gave it to start with. If they are then we have a valid date, if they are not then one or more of the values was outside of the valid range for that part of the date.

The one final thing that we need to watch out for is that when you create a date by passing it the year, month, and day ad numbers, the date object expects the month to be specified as a value between 0 and 11 instead of between 1 and 12. We can build this into our processing so as to allow the month to be entered in the more normal way by having the function subtract one from the supplied month before processing it.

function validDate(y,m,d) {
if (y != parseInt(y,10) || m != parseInt(m,10)
|| d != parseInt(d,10)) return false;
m--;
var nd = new Date(y, m, d);
if (y == nd.getFullYear() && m == nd.getMonth()
&& d == nd.getDate()) return nd;
else return false;
}

Having created our function we can now use it quite simply to both validate the values entered and create a date object from them if they are valid.

var myDate = validDate(year, month, day);
if (!myDate) alert('the date entered is invalid');
else {
// date object myDate has been created and set to the date
}
Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.