Jump to: navigation or main content.

Checking the date

This function takes a value and checks it against a regular expression to see if it is in the correct format (DD/MM/YY). If it is, it then gets each date part and then tests that it is actually a real date. For example, 41/01/2001 is not a real date, despite it being in the correct format.

// Checks a string to see if it in a valid date format
// of (D)D/(M)M/(YY)YY and returns true/false
function isValidDate(s) {
    // format D(D)/M(M)/(YY)YY
    var dateFormat = /^\d{1,4}[\.|\/|-]\d{1,2}[\.|\/|-]\d{1,4}$/;

if (dateFormat.test(s)) { // remove any leading zeros from date values s = s.replace(/0*(\d*)/gi,"$1"); var dateArray = s.split(/[\.|\/|-]/);
// correct month value dateArray[1] = dateArray[1]-1;
// correct year value if (dateArray[2].length<4) { // correct year value dateArray[2] = (parseInt(dateArray[2]) < 50) ? 2000 + parseInt(dateArray[2]) : 1900 + parseInt(dateArray[2]); }
var testDate = new Date(dateArray[2], dateArray[1], dateArray[0]); if (testDate.getDate()!=dateArray[0] || testDate.getMonth()!=dateArray[1] || testDate.getFullYear()!=dateArray[2]) { return false; } else { return true; } } else { return false; } }

How it's done

  1. The first part of the script checks to see if the string is in the correct format. It uses a regular expression to test for the correct formatting and if it doesn't find that, it will return false.
  2. The next part prepares the string by removing any leading zeros on the date parts so we are left with a number
  3. Next, we split up the string to get each of the three date parts (day, month and year) where we subtract one from the month (because in JavaScript 0=January) and then format the date.
  4. Now we build a new test date using the newly formatted date parts mentioned above.
  5. The final step is to compare the new test dates date parts against the originals and if they don't match, then you know that the date was not valid!

Because of the way JavaScript handles date creation, you can ask it to create a date like 'January 32nd 2006' and it will interpret what you mean and convert it into 'February 1st 2006' which is handy to know and can also be used to get future and past dates by simply adding days or months!

Changes

  • 10/06/2009 - Corrected an error with splitting up the date using different separators (-, / and .)
  • 04/02/2008 - Changed it to support different date Separators (DD/MM/YYYY, DD.MM.YYYY and DD-MM-YYYY)
  • 08/03/2007 - Updated to fix some bugs :O

Download the complete JavaScript isDateValid now!

1

Comments (3)

What others have said about this blog post.

  1. 11 Jun 2009 Raphael said:

    Query: You updated the seperators in the regexp but not the split test. So it still wouldn't work for separators other than '/' right?

  2. 11 Jun 2009 Stewart said:

    Yes, you are right! I will correct the javascript so it will work with the other separators.

  3. 21 Sep 2012 Paul said:

    Thanks, it was helpful


This thread has been closed from taking new comments.