Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <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>
              
            
!

CSS

              
                * {
  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;
  }
}
              
            
!

JS

              
                //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";
  }
});
              
            
!
999px

Console