It would be great if someone could point me in the right direction for a school project I am working on. Below you see a super simple timer I made with the help of a tutorial from a youtuber called Web Dev. The example had the New Years eve hardcoded into the script. But I would like to take this one step further and ask the user to enter their special date - so as to make the timer count down to their birthday for example.
So far I have managed to make the counter count down from a given date, not just a hard coded date, and I have managed to create entry forms for user generated data. But once I tried to combine the two something went wrong. I am guessing I need to enter the user generated numbers into one of the functions, not just list them on top as let, var and const?
Oh, and I should ideally figure out a way to make the entry form disappear as soon as the user clicks on the 'enter' button.
For my previous StackOverflow question someone very kindly coded out the entire answer for me. But since this is very much a learning process I would be perfectly happy with a few hints and pointers in the right direction.
let userYear = userYearEntered;
let userMonth = userMonthEntered;
let userDay = userDayEntered;
const userDate = new Date(userYear, userMonth, userDay, 0, 0, 0);
const userOccasion = 'I need to present this project';
function enterSpecialDate() {
var userYearEntered = document.getElementById('userYear').value;
console.log(userYearEntered);
var userMonthEntered = document.getElementById('userMonth').value;
console.log(userMonthEntered);
var userDayEntered = document.getElementById('userDay').value;
console.log(userDayEntered);
}
/*create countdown timer using substraction*/
function updateCountdowntime() {
const currentTime = new Date();
const diff = userDate - currentTime;
console.log(diff);
/* we've been given the difference in milliseconds, so some division is needed */
const d = Math.floor(diff / 1000 / 60 / 60 / 24);
const h = Math.floor(diff / 1000 / 60 / 60) % 24;
const m = Math.floor(diff / 1000 / 60) % 60;
const s = Math.floor(diff / 1000) % 60;
/* send values back into HTML document */
document.getElementById('days').innerHTML = d;
document.getElementById('hours').innerHTML = h;
document.getElementById('minutes').innerHTML = m;
document.getElementById('seconds').innerHTML = s;
document.getElementById('userOccasion').innerHTML = userOccasion;
}
setInterval(updateCountdowntime, 1000);
* {
box-sizing: border-box;
}
body {
background-color: rgb(44, 48, 48);
font-family: sans-serif;
color: rgb(239, 239, 239);
font-size: 3em;
font-weight: 300;
height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
div {
margin-top: 1vh;
}
label {
font-size: 0.4em;
}
<div>
<label for="userYear">year</label>
<input id="userYear" type="number" value="" placeholder="e.g. 2021">
<label for="userMonth">month</label>
<input id="userMonth" type="number" value="" placeholder="e.g. 8">
<label for="userDay">day</label>
<input id="userDay" type="number" value="" placeholder="17">
<button onclick="enterSpecialDate()">Enter</button>
</div>
<div>
<p>there are</p>
<div class="time">
<span id="days"></span>
<span>days</span>
</div>
<div class="time">
<span id="hours"></span>
<span>hours</span>
</div>
<div class="time">
<span id="minutes"></span>
<span>minutes</span>
</div>
<div class="time">
<span id="seconds"></span>
<span>seconds</span>
</div>
<p>until <span id="userOccasion"></span></p>
</div>