0

How to make an automatically calculate of how many day from what user pick a date using datepicker and minus with the current date.

Let's say i have choosen my date using datepicker and automatically calculate for me how many days from date what i pick until todayDateTime = Now()

<style>
    table,tr ,td {border:solid black 1px;}
</style>
<table>
    <!--- Title--->
    <tr>
        <th colspan="2">Duration</th>
    </tr>       
    <tr>
        <th>From</th>
        <th>To</th>
    </tr>
    <!--- Popup calendar Duration From --->
    <tr><td><center>
    <input class="center" type="text" name="Dur_from" size="8"b required="yes">
    <a href="javascript:showCal('Calendar1')"><img align="right" src="calendar_menu.gif"  alt="Select a date" border="0"></a>   
    </center></td>
    <!--- Popup calendar Duration From end --->

    <!--- Popup calendar Duration To --->
    <td><center>
    <input class="center" type="text" name="Dur_to" size="8" required="yes">
    <a href="javascript:showCal('Calendar2')"><img align="right" src="calendar_menu.gif"  alt="Select a date" border="0"></a>
    </center>   
    </td>
    <!--- Popup calendar Duration To end --->
    </tr>
</table>
rrk
  • 15,677
  • 4
  • 29
  • 45
Kijiya
  • 31
  • 9

1 Answers1

0

For jQuery UI datepicker, you can use onSelect callback function to get the handle the date changes and calculate using the following.

$('input[name="Dur_from"], input[name="Dur_to"]').datepicker({
  onSelect: function(dateText, inst) {
    var other, fromDate, toDate
    var fromDate = $('input[name="Dur_from"]').datepicker('getDate');
    var toDate = $('input[name="Dur_to"]').datepicker('getDate');
    if(isValidDate(toDate) && isValidDate(fromDate)){
      // Take the difference between the dates and divide by milliseconds per day.
      // Round to nearest whole number to deal with DST.
      $('#lengthOfService').text(Math.round((Date.parse(toDate)-Date.parse(fromDate))/(1000*60*60*24)));
    }
  }
});
function showCal(cal) { //added the value as ID
  //$('#' + cal).datepicker('show');
}
function isValidDate(str) {
  return !isNaN(Date.parse(str));
}
table,
tr,
td {
  border: solid black 1px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<table>
  <tr>
    <th colspan="2">Duration</th>
  </tr>
  <tr>
    <th>From</th>
    <th>To</th>
  </tr>
  <tr>
    <td>
      <center>
        <input class="center" type="text" name="Dur_from" size="8" b required="yes">
        <a href="javascript:showCal('Calendar1')"><img align="right" src="https://cdn2.iconfinder.com/data/icons/snipicons/5000/calendar-16.png" alt="Select a date" border="0"></a>
      </center>
    </td>
    <td>
      <center>
        <input class="center" type="text" name="Dur_to" size="8" required="yes">
        <a href="javascript:showCal('Calendar2')"><img align="right" src="https://cdn2.iconfinder.com/data/icons/snipicons/5000/calendar-16.png" alt="Select a date" border="0"></a>
      </center>
    </td>
  </tr>
  <tr>
    <td>Length of service:</td>
    <td id="lengthOfService"></td>
  </tr>
</table>
rrk
  • 15,677
  • 4
  • 29
  • 45