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