JavaScript Elapsed Time Between Two Dates

JavaScript Elapsed Time Between Two Dates
Code Snippet:Days Between
Author: WatermelonLesson
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 424
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript code snippet allows you to calculate the elapsed time between two dates in days. It uses a simple form to collect the start date and end date from the user and then uses the JS function to calculate the difference. The result is displayed in a span element on the page.

You can integrate this code into your web projects for instant date difference calculations. It simplifies date-related tasks and streamlines data presentation on your website.

How to Elapsed Time Between Two Dates In JavaScript

1. First, copy the HTML structure to create an intuitive date input interface.

  <h1>Date Calculator</h1>
  <p>How many days are there between two dates?</p>
  <p>(Change a date to automatically calculate the difference)</p>
  <label>Start Date: </label>
  <input type="date" name="start_date">
  <label>End Date: </label>
  <input type="date" name="end_date">

    Time elapsed: 
    <span class="date_span"></span>

2. Now, add the following CSS code to your project to style the basic interface for the date calculator. Feel free to customize the CSS to match your website’s theme. Adjust fonts, colors, and layout to seamlessly integrate the Date Calculator into your design. (Optional)

    flex-direction: column !important;
     background: linear-gradient(to right, #c9d6ff, #e2e2e2) !important;
   line-height: 2.3;
h1 {
  font-family: 'Arial', 'Verdana', sans-serif;
  font-size: 2.5em;
  font-weight: bold;
  text-align: center;

header > p {
  font-family: 'Arial', 'Verdana', sans-serif;
  text-align: center;

div {
  text-align: center;
  border: 0.1em solid #000;

3. Finally, add the following JavaScript code to your website/app project. As users input or change dates, the JavaScript code dynamically computes and displays the elapsed time in days.

let span = document.querySelector(".date_span");
let dateInputs = document.querySelectorAll('input');

dateInputs[1].valueAsDate = new Date();

function calculateElapse() {
  if (!isNaN(dateInputs[0].valueAsNumber) && !isNaN(dateInputs[1].valueAsDate)) { // I don't think this is necessary, but it's robust.
    span.textContent = "" + ((dateInputs[1].valueAsDate - dateInputs[0].valueAsDate) / (24 * 60 * 60 * 1000)) + " day(s)";

dateInputs.forEach(elements => elements.addEventListener('change', calculateElapse));

That’s all! hopefully, you have successfully created a functionality for elapsed time between two dates on your website. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link