Get Days Between Two Dates Calculator using HTML, CSS & JavaScript

Get Days Between Two Dates Calculator using JavaScript

Get Days Between Two Dates Calculator using HTML, CSS & JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Days Between Two Dates Calculator which will show the difference between two dates in exact figure of remaining days. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Days Between Two Dates Calculator Project.

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Days Between Two Dates Calculator Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Days Between Two Dates Calculator Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Days Between Two Dates Calculator Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Days Between Two Dates Calculator

First we’ll start with creating the structure of the Days Between Two Dates Calculator project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Difference Between 2 Dates</title>
    <!-- Google Font -->
    <link
      href="https://fonts.googleapis.com/css2?family=Rubik:wght@400;600&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div class="container">
      <div class="inp-wrapper">
        <div class="date-wrapper">
          <label for="date-1">Start Date</label>
          <input type="date" id="date-1" />
        </div>
        <div class="date-wrapper">
          <label for="date-2">End Date</label>
          <input type="date" id="date-2" />
        </div>
      </div>
      <button id="submit">Submit</button>
      <div id="output">Select the dates to get started</div>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

Multiple Choice Quiz using HTML, CSS & JavaScript

CSS Code for Days Between Two Dates Calculator

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Days Between Two Dates Calculator project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Rubik", sans-serif;
}
body {
  height: 100vh;
  background: linear-gradient(#18f08b 50%, #16191e 50%);
}
.container {
  width: 90vw;
  max-width: 37.5em;
  background-color: #242831;
  padding: 6em 3em;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
}
.inp-wrapper {
  display: flex;
  justify-content: space-around;
  gap: 1.2em;
}
label {
  color: #d5d5d5;
  display: block;
  font-weight: 600;
}
input[type="date"] {
  font-size: 16px;
  padding: 1em;
  color: #242831;
  border: none;
  outline: none;
  border-radius: 0.2em;
  margin-top: 0.6em;
}
::-webkit-calendar-picker-indicator {
  background-color: #18f08b;
  padding: 0.2em;
  cursor: pointer;
  border-radius: 0.1em;
}
button {
  display: block;
  background-color: #18f08b;
  color: #16191e;
  font-size: 18px;
  margin: 1em auto 2em auto;
  border: none;
  padding: 0.7em 2em;
  border-radius: 0.3em;
  font-weight: 600;
}

#output {
  background-color: rgba(255, 255, 255, 0.15);
  text-align: center;
  padding: 1em;
  color: #ffffff;
  font-size: 1.2em;
  letter-spacing: 0.05em;
}
#output span {
  color: #18f08b;
  font-size: 1.4em;
  font-weight: 600;
}
@media screen and (max-width: 550px) {
  .container {
    padding: 4em 2em;
  }
  .inp-wrapper {
    flex-direction: column;
  }
  .date-wrapper {
    display: flex;
    align-items: center;
    flex-direction: column;
  }
}

JavaScript Code for Days Between Two Dates Calculator

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Days Between Two Dates Calculator using HTML, CSS & JavaScript (Source Code).

//Declaring and initializing variables
let submit = document.getElementById("submit");
let output = document.getElementById("output");

submit.addEventListener("click", () => {
  //Create a Date object from input value
  let date1 = new Date(document.getElementById("date-1").value);
  let date2 = new Date(document.getElementById("date-2").value);

  //Check if the input dates are valid
  //If valid calculate the difference
  if (date1.getTime() && date2.getTime()) {
    //Calculate difference in time using getTime function
    //getTime calculates number of years since January 1,1970
    let timeDifference = date2.getTime() - date1.getTime();

    //Since this value is in milliseconds we need to convert it into days
    //We want the difference to be a non-negative number. Hence we use Math.abs()
    let dayDifference = Math.abs(timeDifference / (1000 * 3600 * 24));
    output.innerHTML = `Difference between the two dates is <span>${dayDifference}</span> days`;
  }

  //Else display that the input is valid
  else {
    output.innerHTML = "Please select a valid date";
  }
});

Final Output

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

We have Successfully created our Days Between Two Dates Calculator using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply