How To Get Difference Between Two Dates In Days By Using Javascript ?

Share Me
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares
How To Get Difference Between Two Dates In Days By Using Javascript ?

Hello Dev’s, Today now in this blog, I will show you how we get difference between two dates in days format by using javascript. So here we can know how to simply get difference between two dates in days. So let’s start and let’s we know How To Get Difference Between Two Dates In Days By Using Javascript.

Solution:-

function differenceInDays(dt1, dt2) 
{
    var time =(dt2.getTime() - dt1.getTime());
    var days = time / (1000 * 3600 * 24); 

    return days;
}

Example:-

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Javascript Get Difference Between Two Dates In Days</title>
</head>
<body>
  <script type = "text/javascript">
    function differenceInDays(dt1, dt2) 
     {
        var time =(dt2.getTime() - dt1.getTime());
   
        var days = time / (1000 * 3600 * 24); 
 
        return days;
       
     }
 
    dt1 = new Date("2019-11-30");
    dt2 = new Date("2019-12-03");
 
    var daysDiff = differenceInDays(dt1, dt2)
    document.write( "Javascript Get Difference Between Two Dates In Days :- " + daysDiff ); 
 
  </script>  
</body>
</html>

Output:-

Javascript Get Difference Between Two Dates In Days:-3

Read Also : How to get last 7 days record in laravel?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →