Subtract extracting months between two dates in javascript

1

Hello all GODS OF SCRIPTS , I'm having difficulty extracting the total of months between two dates ex: datefinal (03/29/2018) - datainicial (1/29/2018) = 2 months, however the dates are provided by the jquery datepiker and could inform the command to change the value of the result when changed one or both dates in javascript. Thank you in advance; follow the code below:

<div class="control-form">
   <label for="">Data Inicial:</label>
   <input name="datainicial" type="text" id="calendario" value="29/01/2018"  class="form-control" >
</div>
<div class="control-form">
   <label for="">Data Final:</label>
   <input name="datafinal" type="text" id="calendario2" class="form-control" >
</div>
<div class="control-form">
   <label for="">Numero de Parcelas:</label>
   <input name="numeroparcelas" type="text" class="form-control" id="numeroparcelas">
</div>
    
asked by anonymous 30.01.2018 / 01:58

1 answer

0

The Datepicker jQuery has a onSelect: event when a date is selected. So you can call a function inside that event and calculate:

$('input[id*="calendario"]').datepicker({
   dateFormat: 'dd/mm/yy',
   onSelect: function(){
      var mesIni = $("#calendario").val().split("/");
      var mesFim = $("#calendario2").val().split("/");

      mesIni = new Date(mesIni.pop()+"-"+mesIni[1]+"-"+mesIni.shift());
      mesFim = new Date(mesFim.pop()+"-"+mesFim[1]+"-"+mesFim.shift());

      var meses = mesFim.getMonth() - mesIni.getMonth() 
      + (12 * (mesFim.getFullYear() - mesIni.getFullYear()));

      if(!isNaN(meses)) $("#numeroparcelas").val(meses);
   }
});

See example:

$('input[id*="calendario"]').datepicker({
   dateFormat: 'dd/mm/yy',
   onSelect: function(){
      var mesIni = $("#calendario").val().split("/");
      var mesFim = $("#calendario2").val().split("/");
      
      mesIni = new Date(mesIni.pop()+"-"+mesIni[1]+"-"+mesIni.shift());
      mesFim = new Date(mesFim.pop()+"-"+mesFim[1]+"-"+mesFim.shift());
      
      var meses = mesFim.getMonth() - mesIni.getMonth() 
      + (12 * (mesFim.getFullYear() - mesIni.getFullYear()));
   
      if(!isNaN(meses)) $("#numeroparcelas").val(meses);
   }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><linkrel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script><divclass="control-form">
   <label for="">Data Inicial:</label>
   <input name="datainicial" type="text" id="calendario" value="29/01/2018"  class="form-control" >
</div>
<div class="control-form">
   <label for="">Data Final:</label>
   <input name="datafinal" type="text" id="calendario2" class="form-control" >
</div>
<div class="control-form">
   <label for="">Numero de Parcelas:</label>
   <input name="numeroparcelas" type="text" class="form-control" id="numeroparcelas">
</div>
    
30.01.2018 / 02:35