I referred earlier post for Date comparison from stack overflow - Compare two dates with JavaScript , yet I am not sure where I am wrong . Please guide.
function dateValidation(){
alert("dateValidation");
var dateFlag=true;
var fromdate_1=new Date(fYear,fMonth-1,fDate);
var currentDate_1=new Date();
var todate_1=new Date(tYear,tMonth-1,tDate);
alert("fromDate"+fromdate_1);
alert("toDate"+todate_1);
alert("currentDate"+currentDate_1);
if(fDate==null||fMonth==null||fYear==null)
{
alert("null");
dateFlag=false;
fromDateFlag=false;
}
else
{
alert("else");
if(!isNaN(fromdate_1) && !isNaN(currentDate_1) && !isNaN(todate_1))
{
alert("else 2");
if(fromdate_1 > currentDate_1)
{
alert("fromDate>currentDate");
fgcFlag=false;
dateFlag=false;
}
if(todate_1 < fromdate_1)
{
alert("toDate<fromDate");
fgtFlag=false;
dateFlag=false;
}
if(fromdate_1 > currentDate_1 && todate_1 < fromdate_1)
{
alert("fromDate>currentDate && toDate< fromDate");
dateFlag=false;
tlfFlag=false;
}
if(fromdate_1 > currentDate_1 && todate_1 > fromdate_1)
{
alert("fromDate>currentDate && toDate> fromDate");
fgcFlag=false;
dateFlag=false;
}
}
}
alert("dateFlag"+dateFlag);
return dateFlag;
}
On selection of fromdate - "Aug 1st 2016" and todate - "Aug 25th 2016" and when I invoke the javascript -dateValidation, I see the alerts in the below order
- dateValidation
- fromDate "Aug 1st 2016"
- toDate "Aug 16th 2016"
- currentDate "Aug 25th 2016"
- else
- else 2
- dateFlag true
I observe the date comparison is not happening in javascript. I am aware dates.compare(a,b)
Returns a number:
- -1 if a < b
- 0 if a = b
- 1 if a > b
- NaN if a or b is an illegal date
Please could anyone point where I am going wrong / what changes must be made ?