Check date function to check a date is exist or not in javascript

A situation suppost you want to check a date is exist in any given month or not so below a simple javascript function which is very similar to the php checkdate(). It can help you in it -

Below is the javascript function :

function checkdate(m, d, y) {
 return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0))
    .getDate();
}



Below Is Example :

  example 1 :  checkdate(12, 31, 2000);
  returns 1  : true
(This will return true because this is correct date format)
 
  example 2  :  checkdate(2, 29, 2001);
  returns 2   : false (This will return false because in 2001 feb is 28 days)

 
  example 3  :  checkdate(3, 31, 2008);
  returns 3   : true
(This will return true because this is the correct date of march month)
 
  example 4  :  checkdate(1, 390, 2000);
  returns 4   : false
(This will return false because 390 not a day of any month)

If you like this post don't forgot to leave a comment.

Chears..
Happy Coding :)

Post a Comment

Previous Post Next Post