update timer function
This commit is contained in:
parent
151063cd76
commit
b50ffcad2e
1 changed files with 57 additions and 21 deletions
76
index.html
76
index.html
|
@ -86,35 +86,71 @@
|
||||||
</footer>
|
</footer>
|
||||||
</body>
|
</body>
|
||||||
<script>
|
<script>
|
||||||
// Set the date we're counting up from
|
function getTimeSince(dateString) {
|
||||||
const countUpDate = new Date("Nov 1, 2021 16:00:00").getTime();
|
// Parse the input date string into a Date object
|
||||||
const timerElement = document.getElementById("timer")
|
const startDate = new Date(dateString);
|
||||||
|
|
||||||
function updateHtml() {
|
// Get the current date
|
||||||
// Get today's date and time
|
const currentDate = new Date();
|
||||||
var now = new Date().getTime();
|
|
||||||
|
|
||||||
// Find the distance between now and the count up date
|
// Calculate the differences in each unit
|
||||||
var distance = now - countUpDate;
|
let years = currentDate.getFullYear() - startDate.getFullYear();
|
||||||
|
let months = currentDate.getMonth() - startDate.getMonth();
|
||||||
|
let days = currentDate.getDate() - startDate.getDate();
|
||||||
|
let hours = currentDate.getHours() - startDate.getHours();
|
||||||
|
let minutes = currentDate.getMinutes() - startDate.getMinutes();
|
||||||
|
let seconds = currentDate.getSeconds() - startDate.getSeconds();
|
||||||
|
|
||||||
// Time calculations for days, hours, minutes and seconds
|
// Adjust for negative month difference
|
||||||
var years = Math.floor(distance / (1000 * 60 * 60 * 24 * 365));
|
if (months < 0) {
|
||||||
var days = Math.floor(distance % (1000 * 60 * 60 * 24 * 365) / (1000 * 60 * 60 * 24));
|
years--;
|
||||||
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
|
months += 12;
|
||||||
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
|
}
|
||||||
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
|
|
||||||
|
|
||||||
// Display the result in the element with id="timer"
|
// Adjust for negative day difference
|
||||||
timerElement.innerHTML = years + "y " + days + "d " + hours + "h "
|
if (days < 0) {
|
||||||
+ minutes + "m " + seconds + "s";
|
months--;
|
||||||
|
let prevMonth = new Date(currentDate.getFullYear(), currentDate.getMonth(), 0).getDate();
|
||||||
|
days += prevMonth;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust for negative hour difference
|
||||||
|
if (hours < 0) {
|
||||||
|
days--;
|
||||||
|
hours += 24;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust for negative minute difference
|
||||||
|
if (minutes < 0) {
|
||||||
|
hours--;
|
||||||
|
minutes += 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust for negative second difference
|
||||||
|
if (seconds < 0) {
|
||||||
|
minutes--;
|
||||||
|
seconds += 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
years: years,
|
||||||
|
months: months,
|
||||||
|
days: days,
|
||||||
|
hours: hours,
|
||||||
|
minutes: minutes,
|
||||||
|
seconds: seconds
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateHtml() {
|
||||||
|
var result = getTimeSince("Nov 1, 2021 16:43:22")
|
||||||
|
const timerElement = document.getElementById("timer")
|
||||||
|
timerElement.innerHTML = `${result.years} Years, ${result.months} Months, ${result.days} Days,<br/> ${result.hours} Hours, ${result.minutes} Minutes, ${result.seconds} Seconds`
|
||||||
|
}
|
||||||
|
|
||||||
updateHtml()
|
updateHtml()
|
||||||
|
|
||||||
// Update the count down every 1 second
|
var x = setInterval(updateHtml, 999);
|
||||||
var x = setInterval(updateHtml, 1000);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</html>
|
</html>
|
Loading…
Reference in a new issue