0

I'm making simple application for record internal leaves of my office. with this application I have to provide users to two HTML date fields for select start leave date and end leave date. At the same time I need to calculate how many days have in between user's selection also this count may exclude the weekdays. To do this i tried to use simple javascript with one of date fields onchange event but it's not working

this is the complete code with HTML I have tried

<html >
<head>
</head>
<body>
 <form action='reqprocess.php' method='post'>
            <td><input type="date" name="datepicker" class='form-control' id="startd" required/> <br>
            <td><input type="date" name="datepicker2" class='form-control' id="endd"required /> <br>
            <td><input type="text" name='leavehmd' class='form-control' id="lhmd"/><br>
            <input type='submit' value='Apply' class='btn btn-primary' />
</form>

        <script type="text/javascript">

        var chng1 = document.getElementById("endd");
         chng1.onchange = function () {
             var date1 = Date.parse(document.getElementById("startd").value);
             var date2 = Date.parse(document.getElementById("endd").value);
             if (date1 && date2) {
                 var timeDiff = Math.abs(date2.getTime() - date1.getTime());
                 var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
                 //alert(diffDays);
                 document.getElementById("lhmd").value = diffDays;
             }
         }

         var chng2 = document.getElementById("startd")
        chng2.onchange = function () {
             var date1 = Date.parse(document.getElementById("startd").value);
             var date2 = Date.parse(document.getElementById("endd").value);
             if (date1 && date2) {
                 var timeDiff = Math.abs(date2.getTime() - date1.getTime());
                 var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
                 //alert(diffDays);
                 document.getElementById("lhmd").value = diffDays;
             }
         }

        </script>
</body>
</html>

But when sect second date, on-change function not works and Input field suppose to fill with counted dates not populated. How can I fix this ?

or how can I achieve this via methods like ajax or jquery

rafalefighter
  • 714
  • 2
  • 11
  • 39

1 Answers1

1

You need to make sure there is a value in each input field before attempting to calculate the difference

You can check this by setting a conditional with the values as the operands. They will evaluate to falsy if there is no value and truthy if there is a value. If both values are present, you can then calculate the difference.

The linked duplicate question has a good clean way to count days between dates:

function parseDate(str) {
    var mdy = str.split('/');
    return new Date(mdy[2], mdy[0]-1, mdy[1]);
}

function daydiff(first, second) {
    return Math.round((second-first)/(1000*60*60*24));
}


var chng1 = document.getElementById("endd");
var chng2 = document.getElementById("startd");
chng1.onchange = displayCurrentDayDifference();
chng2.onchange = displayCurrentDayDifference();

function displayCurrentDayDifference() {
    var date1 = document.getElementById("startd").value;
    var date2 = document.getElementById("endd").value;
    if (date1 && date2) {
        var daysBetween = daydiff(parseDate($('#startd').val()), parseDate($('#endd').val()));
        document.getElementById("lhmd").value = daysBetween;
    }
}
Jecoms
  • 2,558
  • 4
  • 20
  • 31