How do I get the number of days between two dates in JavaScript?

JavascriptWeb DevelopmentFront End Technology

To get dates in JavaScript, use the getTime() method. For getting the difference, calculate the difference between dates.

Example

You can try to run the following code to learn how to calculate a difference between two dates −

Live Demo

<!DOCTYPE html>
<html>
   <body>
      <script>
         var dateFirst = new Date("10/29/2017");
         var dateSecond = new Date("10/18/2017");

         // time difference
         var timeDiff = Math.abs(dateSecond.getTime() - dateFirst.getTime());

         // days difference
         var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

         // difference
         alert(diffDays);
      </script>
   </body>
</html>
raja
Published on 03-Feb-2018 16:47:55
Advertisements