0

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

  1. dateValidation
  2. fromDate "Aug 1st 2016"
  3. toDate "Aug 16th 2016"
  4. currentDate "Aug 25th 2016"
  5. else
  6. else 2
  7. 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 ?

Community
  • 1
  • 1
Sarronya
  • 353
  • 2
  • 8
  • 19
  • Use the ms after 1970, `Date.getTime()` to compare dates. – Shilly Aug 16 '16 at 14:23
  • Oh oki.. so it works as expected – Sarronya Aug 16 '16 at 14:32
  • Then it's normal it doesn't work. 25 aug as the current date is bigger than both 1aug and 16 aug. How do you get 25 aug though, since if I copy/paste your code into my console, I get 16 aug 16:33 – Shilly Aug 16 '16 at 14:33
  • Hmm, the logic seems flawed I guess. fromDate (1 aug) is not bigger than currentDate (16 aug), so condition 1,3 and 4 will not trigger. And toDate (25aug) isn't smaller than fromDate (1 aug) either, so condition 2 won't trigger either. So edit your logic to match what you're trying to do. – Shilly Aug 16 '16 at 14:40
  • You can greatly simplify the date validation, see [*How to validate a date?*](http://stackoverflow.com/questions/5812220/how-to-validate-a-date/5812341#5812341). Once you've validated the dates, to see if "today" is in range, you just need `fromDate <= today && today <= toDate`. The comparison operators coerce the Date objects to their time value. – RobG Aug 17 '16 at 23:13
  • BTW, it would help greatly if you post the code as a runnable snippet with valid input. – RobG Aug 17 '16 at 23:17

1 Answers1

0

You get dateFlag true because that is the value it's initialised to, and all of the if tests inside else2 fail, so the value of dateFlag is not changed.

Given:

  1. fromDate "Aug 1st 2016"
  2. toDate "Aug 16th 2016"
  3. currentDate "Aug 25th 2016"

then:

fromdate > currentDate     // false
todate   < fromdate        // false
fromdate > currentDate ... // false, second part of test isn't reached
fromdate > currentDate ... // false, second part of test isn't reached

So the code does exactly what it's been told to do. If you are trying to see if currentDate is between fromDate and toDate, then:

if (fromDate <= currentDate && currentDate <= toDate) {
  // currentDate is in range
}

does that in one step. But you haven't said what you are actually trying to do.

RobG
  • 142,382
  • 31
  • 172
  • 209