0
<input type="date" id="startDate" />
<input type="date" id="endDate" />

<script>
   $("#endDate").click(function () {
        var date1 = $("#startDate").val();
        var date2 = $("#endDate").val();
        var timeDiff = Math.abs(date2.getTime() - date1.getTime());
        var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
        alert(diffDays)​;
    });
</script>

getTime not work , and this message shown intellisense was unable to determine an accurate completion list for this expression How do I get the difference days between two Dates in this case and how can getTime work with me ?

Lukasz Koziara
  • 4,274
  • 5
  • 32
  • 43
  • possible duplicate of [How do I get the difference between two Dates in JavaScript?](http://stackoverflow.com/questions/41948/how-do-i-get-the-difference-between-two-dates-in-javascript) – Yaje Jul 22 '14 at 08:35
  • You need to make `Date` objects; right now `date1` & `date2` are just strings. See: http://stackoverflow.com/questions/10804042/calculate-time-difference-with-javascript/10804367#10804367 – Martin Tournoij Jul 22 '14 at 08:35

1 Answers1

2

This is a simple implementation:

http://jsfiddle.net/xtY47/

 $("#endDate").click(function () {
   var date1 = new Date($("#startDate").val()); //the value is yyyy-MM-dd
   var date2 = new Date($("#endDate").val());
   console.log(date2); 
    var timeDiff = Math.abs(date2.getTime() - date1.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 
    alert(diffDays);
});
Lukasz Koziara
  • 4,274
  • 5
  • 32
  • 43
chf
  • 743
  • 4
  • 14