2
Answers

how to calculate number of months and days in javascript

Hi,
I want to calculate number of months and days between two dates using javascript.
 
Javascript
function calculate(){
      var datefrom = $("#datefrom").val();
      var dateto = $("#dateto").val();
      var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
      var firstDate = new Date(datefrom);
      var secondDate = new Date(dateto);
      var days = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));
      console.log(days);
 
 html
 
<p><strong>Date From:</strong><input type="text" name="datefrom" id="datefrom"></p>
<p><strong>Date To:</strong><input type="text" name="dateto" id="dateto"></p>
<p><input type="button" name="calculate" id="calculate" onclick="calculate();" value="Total"></p> 
 
Answers (2)