personal-website/layouts/shortcodes/footer-timer.html

65 lines
1.9 KiB
HTML
Raw Normal View History

2024-05-23 07:06:41 +02:00
^-^ <a id="inconspicuous-timer-a" href="/images/secret.jpg"><i id="inconspicuous-timer"></i></a>
2024-05-03 16:26:31 +02:00
<script>
2024-05-23 07:06:41 +02:00
function getTimeSince(dateString) {
// Parse the input date string into a Date object
const startDate = new Date(dateString);
// Get the current date
const currentDate = new Date();
// 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();
// Adjust for negative month difference
if (months < 0) {
years--;
months += 12;
}
// 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;
}
return {
years: years,
months: months,
days: days,
hours: hours,
minutes: minutes
};
}
2024-05-03 16:26:31 +02:00
// Set the date we're counting up from
2024-05-23 07:06:41 +02:00
const startDate = new Date("Dec 28, 2023 16:34:56");
2024-05-03 16:26:31 +02:00
function updateHtml() {
2024-05-23 07:06:41 +02:00
const result = getTimeSince("Dec 28, 2023 16:34:56");
const timerElement = document.getElementById("inconspicuous-timer")
timerElement.innerHTML = `${result.years}y ${result.months}m ${result.days}d ${result.hours}h ${result.minutes}min`;
2024-05-03 16:26:31 +02:00
}
2024-05-23 07:06:41 +02:00
2024-05-03 16:26:31 +02:00
updateHtml()
2024-05-23 07:06:41 +02:00
var x = setInterval(updateHtml, 10000);
2024-05-03 16:26:31 +02:00
</script>