0

I want to extract the data from array of objects in range of start date and end date, but I m not getting desired result, like I want only persons data that falls in range of start date and end date as I m using HTML5 input date type for taking start and end date. Code is like this

<script type="text/javascript">

    var employees = [
       {
           "firstName": "John",
           "lastName": "Doe",
           "hiringdate": "02-01-2016"
       },
       {
           "firstName": "Anna",
            "lastName": "Smith",
           "hiringdate": "03-01-2016"
       },
       {
            "firstName": "Peter",
           "lastName": "Jones",
           "hiringdate": "05-01-2016"
       }
    ];

    function myfnc()
    {
      var i;
       var sd = new Date(document.getElementById("value1").value);
       var ld = new Date(document.getElementById("value2").value);
       document.getElementById("demo").innerHTML = "";
   var gd;

       for (i = 0; i <employees.length; i++)
       {
            gd= new Date(employees[i].hiringdate);


           if ((gd>=sd) && (gd <= sd))
           {
                document.getElementById("demo").innerHTML +="First Name: "+employees[i].firstName + " " +"Last Name: "+employees[i].lastName+"<br>";
           }
          else
           {
                document.getElementById("demo").innerHTML = "No Record Found";
           } 
         }

       }
</script>
  • I have searched enough on your website but couldn't understood that's why I posted question. – Adeep Malmotra Jun 03 '16 at 09:27
  • You might have a problem converting the string to Dates. But also, you need to refactor your code to move the `document.getElementById("demo").innerHTML = "No Record Found";` outside the for loop... – AJPerez Jun 03 '16 at 09:27
  • @AdeepMalmotra You need to convert string to date type first and then compare dates like given here http://stackoverflow.com/questions/492994/compare-two-dates-with-javascript – Krunal Jun 03 '16 at 09:30

0 Answers0