SA__ SA__ - 22 days ago 9
Javascript Question

How to allow only for dates that are equal and smaller

Here is my code to find whether the two given dates are equal or not ..

It should allow if today is small or equal. and it should not allow if it is greater date.

var date = '10-11-2015';
var today = '11-11-2016'
alert(today)
alert(date)
if( today <= date )
{
alert("small-or-equal-allow")
}
else
{
alert("larger-not-allow")
}


But its showing not working as expected for few days. What is the mistake and how can i fix it ?

Answer

You're comparing strings, not dates. If you want the values to be treated as dates then you need to cast them as dates...

var date = new Date('10-11-2015');
var today = new Date('11-11-2016');

if (today <= date) {
   alert("small-or-equal-allow");
}
else {
   alert("larger-not-allow");
}

Comments