3

In php there is a checkdate (month, day, year) function which helps in checking whether the date is a proper date or not. Does a similar function exist in JavaScript or jQuery?

user564205
  • 31
  • 2

1 Answers1

3

You could easily create your own one, but you have to know how Javascript handle's non-exisiting dates:

new Date(2010, 14, 34); // gives Sun Apr 03 2011, it just counts on.

So, this would do the trick:

function checkDate(year, month, day){
  var d = new Date(year, month, day);

  return d.getFullYear() == year && 
         d.getMonth() == month &&
         d.getDate() == day;
}
Harmen
  • 22,092
  • 4
  • 54
  • 76
  • Or if you *really* want to go the PHP way, you can resort to phpjs.org's solution: http://phpjs.org/functions/checkdate:366 – nikc.org Jan 05 '11 at 17:01