[javascript] How do I get the number of days between two dates in JavaScript?

A Better Solution by

Ignoring time part

it will return 0 if both the dates are same.

_x000D_
_x000D_
function dayDiff(firstDate, secondDate) {_x000D_
  firstDate = new Date(firstDate);_x000D_
  secondDate = new Date(secondDate);_x000D_
  if (!isNaN(firstDate) && !isNaN(secondDate)) {_x000D_
    firstDate.setHours(0, 0, 0, 0); //ignore time part_x000D_
    secondDate.setHours(0, 0, 0, 0); //ignore time part_x000D_
    var dayDiff = secondDate - firstDate;_x000D_
    dayDiff = dayDiff / 86400000; // divide by milisec in one day_x000D_
    console.log(dayDiff);_x000D_
  } else {_x000D_
    console.log("Enter valid date.");_x000D_
  }_x000D_
}_x000D_
_x000D_
$(document).ready(function() {_x000D_
  $('input[type=datetime]').datepicker({_x000D_
    dateFormat: "mm/dd/yy",_x000D_
    changeMonth: true,_x000D_
    changeYear: true_x000D_
  });_x000D_
  $("#button").click(function() {_x000D_
    dayDiff($('#first').val(), $('#second').val());_x000D_
  });_x000D_
});
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">_x000D_
<script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>_x000D_
_x000D_
<input type="datetime" id="first" value="12/28/2016" />_x000D_
<input type="datetime" id="second" value="12/28/2017" />_x000D_
<input type="button" id="button" value="Calculate">
_x000D_
_x000D_
_x000D_