Javascript Difference Between Two Dates

How To Get Days Difference Between 2 Date Using Javascript  

Difference Between 2 Date Using Javascript


In this JavaScript tutorial we will see how to get the number of days between two dates using Math.floor and the input type date in JS and Netbeans Editor .


Project Source Code:

    
<!DOCTYPE html>
<html>
    <head>
        <title>Javascript: Days Difference Between 2 Date </title>
        <meta charset="windows-1252">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body>
        
        <input type="date" id="startDate">
        <input type="date" id="endDate">
        <button onclick="dateDiff()" >Diff</button>
        
        <script>
            
            function dateDiff(){
                
                var date1 = new Date(document.getElementById("startDate").value),
                    date2 = new Date(document.getElementById("endDate").value);
                     // number of milliseconds in one day (24H)
             var diffResult = Math.floor((date2 - date1) / (1000 * 60 * 60 * 24));
             
             console.log(diffResult);
                
            }
            
        </script>
        
    </body>
</html>




OUTPUT:



2 date diff in js
Result : 365

Share this

Related Posts

Previous
Next Post »