JavaScript Difference Between Two Dates In Days Example


Share:
Published By: Admin, Published On: Aug 23, 2021, Category: Javascript


Hello Friends,

Now let's see example of how to use javascript get difference between two dates in days example. We will use how to use if get difference between two dates in days in javascript. Here you will learn how to use javascript get difference between two dates in days. This is a short guide on get difference between two dates in days. Let's get started with how to get difference between two dates in days in javascript.

Here i will give you many example how you can check javascript get difference between two dates in days.

Example: 1
<!DOCTYPE html>
<html>
    <head>
        <title>JavaScript difference between two dates in days</title>
    </head>
    <body>
        <script>
            var dateFirst = new Date("11/25/2017");
            var dateSecond = new Date("11/28/2017");
            
            var timeDiff = Math.abs(dateSecond.getTime() - dateFirst.getTime());
            var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
            alert(diffDays);
        </script>
    </body>
</html>
Output:
3
Example: 2
<!DOCTYPE html>
<html>
    <head>
      <meta charset="utf-8">
      <title>JavaScript difference between two dates in days</title>
      <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    </head>
    <body class="bg-dark">
        <div class="container mt-5">
            <div class="row">
                <div class="col-md-6 offset-md-3">
                    <div class="card">
                        <div class="card-header">
                            <h5>JavaScript difference between two dates in days</h5>
                        </div>
                        <div class="card-body">
                            <p>DAYS BETWEEN GIVEN DATES</p>
                            <p id="cmp"></p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <script>
            var day_start = new Date("Jan 01 2016");
            var day_end = new Date("Dec 31 2016");
            var total_days = (day_end - day_start) / (1000 * 60 * 60 * 24);
            document.getElementById("cmp").innerHTML = Math.round(total_days);
        </script>
    </body>
</html>
Output:
DAYS BETWEEN GIVEN DATES
365

It will help you....

Share: