Widget:CountdownStarPath: Difference between revisions
Jump to navigation
Jump to search
Marblemadmax (talk | contribs) No edit summary |
Marblemadmax (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
<div id="countdown- | <div id="countdown-specific"></div> | ||
<script> | <script> | ||
(function() { | (function() { | ||
// Function to update the countdown | // Function to update the specific countdown to March 15, 2pm UTC | ||
function | function updateSpecificCountdown() { | ||
var currentDate = new Date(); | var currentDate = new Date(); | ||
var targetDate = new Date( | var targetDate = new Date(Date.UTC(currentDate.getUTCFullYear(), 2, 15, 14, 0, 0)); // March 15, 2pm UTC | ||
// If current date is past this year's target, move target to next year | |||
if (currentDate > targetDate) { | |||
targetDate.setUTCFullYear(currentDate.getUTCFullYear() + 1); | |||
} | |||
var timeDifference = targetDate - currentDate; | var timeDifference = targetDate - currentDate; | ||
var days = Math.floor(timeDifference / (24 * 60 * 60 * 1000)); | var days = Math.floor(timeDifference / (24 * 60 * 60 * 1000)); | ||
Line 23: | Line 22: | ||
var countdownText = ''; | var countdownText = ''; | ||
if ( | if (timeDifference <= 24 * 60 * 60 * 1000) { | ||
countdownText = | // Format as H:MM:SS for the last 24 hours | ||
countdownText = hours + ':' + | |||
('0' + minutes).slice(-2) + ':' + | |||
('0' + seconds).slice(-2); | |||
} else { | } else { | ||
countdownText = | // Else, format as Xd Xh Xm for more than 24 hours | ||
countdownText = days + 'd ' + | |||
hours + 'h ' + | |||
minutes + 'm'; | |||
} | } | ||
document.getElementById('countdown- | document.getElementById('countdown-specific').innerHTML = countdownText; | ||
// | // Use a separate setTimeout to avoid clashing with other scripts | ||
setTimeout( | setTimeout(updateSpecificCountdown, 1000); | ||
} | } | ||
// | // Ensure this does not conflict with window.onload used by other scripts | ||
window. | if (window.addEventListener) { | ||
window.addEventListener('load', updateSpecificCountdown, false); | |||
}; | } else if (window.attachEvent) { | ||
window.attachEvent('onload', updateSpecificCountdown); | |||
} else { | |||
document.addEventListener('DOMContentLoaded', updateSpecificCountdown, false); | |||
} | |||
})(); | })(); | ||
</script> | </script> |
Latest revision as of 20:56, 26 February 2024
<script> (function() {
// Function to update the specific countdown to March 15, 2pm UTC function updateSpecificCountdown() { var currentDate = new Date(); var targetDate = new Date(Date.UTC(currentDate.getUTCFullYear(), 2, 15, 14, 0, 0)); // March 15, 2pm UTC
// If current date is past this year's target, move target to next year if (currentDate > targetDate) { targetDate.setUTCFullYear(currentDate.getUTCFullYear() + 1); }
var timeDifference = targetDate - currentDate;
var days = Math.floor(timeDifference / (24 * 60 * 60 * 1000)); var hours = Math.floor((timeDifference % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000)); var minutes = Math.floor((timeDifference % (60 * 60 * 1000)) / (60 * 1000)); var seconds = Math.floor((timeDifference % (60 * 1000)) / 1000);
var countdownText = ;
if (timeDifference <= 24 * 60 * 60 * 1000) { // Format as H:MM:SS for the last 24 hours countdownText = hours + ':' + ('0' + minutes).slice(-2) + ':' + ('0' + seconds).slice(-2); } else { // Else, format as Xd Xh Xm for more than 24 hours countdownText = days + 'd ' + hours + 'h ' + minutes + 'm'; }
document.getElementById('countdown-specific').innerHTML = countdownText;
// Use a separate setTimeout to avoid clashing with other scripts setTimeout(updateSpecificCountdown, 1000); }
// Ensure this does not conflict with window.onload used by other scripts if (window.addEventListener) { window.addEventListener('load', updateSpecificCountdown, false); } else if (window.attachEvent) { window.attachEvent('onload', updateSpecificCountdown); } else { document.addEventListener('DOMContentLoaded', updateSpecificCountdown, false); }
})(); </script>