JavaScript

Calculate the number of days between two dates in Javascript

In this tutorial, we are going to see how to calculate the difference between two dates using the getTime() method of Javascript.
 

Script to calculate the difference between two dates
<!DOCTYPE>
<html>
   <body>
      <script type="text/javascript">
         var date1 = new Date("12/12/2020");
         var date2 = new Date("12/12/2021");
         // time difference
         var time_diff = date2.getTime() - date1.getTime();
         // days difference
         var days_Diff = time_diff / (1000 * 3600 * 24);
         // Display the difference
         alert(days_Diff);
      </script>
   </body>
</html>

Output:

365
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *