Syed Syed - 1 month ago 6
Javascript Question

Validating date with current date using jquery

I'm validating to check my passed date with current date.. but in all cases it returns false. Here is my code

function validateDate(date) {
var curdate= new Date();
curdate.setHours(0,0,0,0);
if(new Date(date) > curdate) {
console.log(" greater");
} else { console.log(" lesser"); };

}


I'm passing date as 24-10-2016

What is wrong with the code and I should not use date picker..

Answer

The format you are passing is not the standard ISO date format. The ISO format is YYYY-MM-DD so you need to pass date string as 2016-10-24

var dateString = '2016-10-28'; //28th Oct 2016

validateDate(dateString);

function validateDate(date) {
       var curdate= new Date();
       curdate.setHours(0,0,0,0);
       if(new Date(date) > curdate) { 
             console.log(" greater"); 
       } else { console.log(" lesser"); };

}

Comments