1

The user may choose a start date and end date for holiday. But, a problem may occur if the user choose an end date smaller than the start date.

        Start Date :  
        <input type="date" class="form-control" name="startDate" id="from" required/>
        End Date : 
        <input type="date" class="form-control" name="endDate" id="to" required/>
        <br>

        <input type ="hidden" name="empID" value="<%=empID%>">
        <input type="hidden" name="activity" value="mobilityType"/>
        <input type="hidden" name="status" value="pending"/>
        <input class="btn btn-lg btn-primary btn-block" type="submit" value="Submit"> 
        <input class="btn btn-lg btn-danger btn-block" type="reset" value="Reset">          

A code to alert the user, if the user select an end date smaller than start date should be like:

  var from = $("#from").val();
  var to = $("#to").val();

  if(Date.parse(from) > Date.parse(to)){
      alert("Invalid Date Range");
  }else{
      alert("Valid date Range");
  }

Which should be the correct way to compare start date and end date?

infiniteLearner
  • 3,555
  • 2
  • 23
  • 32
  • 1
    What you have here should work as written. What more are you looking for? – David Krider Nov 27 '19 at 13:39
  • So what is not working with what you tried. I assume that JavaScript code lives inside some function you call.... – epascarello Nov 27 '19 at 16:15
  • Firstly you need to properly parse the timestamps, `Date.parse(to)` is not a good way to do that, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Nov 27 '19 at 22:05

0 Answers0