Sandeep Chaurasia

Sandeep Chaurasia

  • NA
  • 38
  • 7.8k

how to calculate number of months and days in javascript

Feb 2 2017 6:47 AM
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)