Skip to content

Commit

Permalink
Merge pull request pranjay-poddar#348 from Rakesh9100/calc
Browse files Browse the repository at this point in the history
Adding Number of Days Calculator
  • Loading branch information
pranjay-poddar authored Jul 31, 2022
2 parents 3d24c21 + 4c228bf commit 08483de
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 0 deletions.
27 changes: 27 additions & 0 deletions Js-Projects/Number_of_Days_Calculator/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//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 = `<span>${dayDifference}</span> days between both the dates`;
}

//Else display that the input is valid
else {
output.innerHTML = "Please select a valid date";
}
});
29 changes: 29 additions & 0 deletions Js-Projects/Number_of_Days_Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Number of Days Between Two Dates</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="container">
<div class="heading">Calculate No. of Days Between Two Dates</div>
<div class="inp-wrapper">
<div class="date-wrapper">
<label for="date-1">Please Enter First Date</label>
<input type="date" id="date-1" />
</div>
<div class="date-wrapper">
<label for="date-2">Please Enter Second Date</label>
<input type="date" id="date-2" />
</div>
</div>
<button id="submit">Calculate</button>
<div id="output">Select the dates to get started</div>
</div>

</body>
<script src="app.js"></script>
</html>
89 changes: 89 additions & 0 deletions Js-Projects/Number_of_Days_Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Rubik", sans-serif;
}
body {
height: 100vh;
background: rgb(245, 239, 66);
}
.container {
width: 70vw;
max-width: 37.5em;
background-color: #f6f9fa;
padding: 3em 1em;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
border-radius: 2rem;
}
.heading{
background: rgb(18, 128, 207);
color: white;
margin: -48px -16px 50px -16px;
font-size: 23px;
padding: 5px;
text-align: center;
}
.inp-wrapper {
display: flex;
justify-content: space-around;
gap: 1.2em;
}
label {
color: #0f1e32;
display: block;
font-weight: 600;
}
input[type="date"] {
font-size: 16px;
padding: 1em;
color: #242831;
border: 2px solid rgb(7, 108, 147);
outline: none;
border-radius: 0.2em;
margin-top: 0.6em;
}
::-webkit-calendar-picker-indicator {
background-color: #7eceee;
padding: 0.2em;
cursor: pointer;
border-radius: 0.1em;
}
button {
display: block;
background-color: #1a78db;
color: #ffffff;
font-size: 18px;
margin: 2.2em auto 2em auto;
border: none;
padding: 0.7em 2em;
border-radius: 0.3em;
font-weight: 500;
}

#output {
background-color: rgba(255, 255, 255, 0.15);
text-align: center;
padding: 1em;
margin: 0px 30px 0px 30px;
color: #0a49c7;
font-size: 1.2em;
letter-spacing: 0.05em;
box-shadow: 0 0 20px rgba(0,139,253,0.45);
}
#output span {
color: #18f08b;
font-size: 1.4em;
font-weight: 600;
}
.inp-wrapper {
flex-direction: column;
}
.date-wrapper {
display: flex;
align-items: center;
flex-direction: column;
}

0 comments on commit 08483de

Please sign in to comment.