简体   繁体   中英

How do I get the difference days between two Dates in JavaScript?

<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 ?

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);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM