Widget:CountdownStarPath: Difference between revisions

From Dreamlight Valley Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
<div id="countdown-starpath"></div>
<div id="countdown-specific"></div>


<script>
<script>
(function() {
(function() {
   // Function to update the countdown widget
   // Function to update the specific countdown to March 15, 2pm UTC
   function updateCountdownWidget() {
   function updateSpecificCountdown() {
     var currentDate = new Date();
     var currentDate = new Date();
     var targetDate = new Date("2024-02-29T12:00:00-05:00"); // Replace with your target date and time
     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;
    if (timeDifference <= 0) {
      // Countdown has elapsed, do not reset
      document.getElementById('countdown-widget').innerHTML = "Countdown Ended";
      return;
    }


     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 (days >= 1) {
     if (timeDifference <= 24 * 60 * 60 * 1000) {
       countdownText = days + 'd ' + hours + 'h ' + minutes + 'm';
      // Format as H:MM:SS for the last 24 hours
    } else if (hours >= 1) {
       countdownText = hours + ':' +
      countdownText = hours + 'h ' + minutes + 'm';
                      ('0' + minutes).slice(-2) + ':' +
    } else if (minutes >= 1) {
                      ('0' + seconds).slice(-2);
      countdownText = minutes + 'm';
     } else {
     } else {
       countdownText = (seconds < 10 ? '0' : '') + seconds + 's';
      // Else, format as Xd Xh Xm for more than 24 hours
       countdownText = days + 'd ' +
                      hours + 'h ' +
                      minutes + 'm';
     }
     }


     document.getElementById('countdown-starpath').innerHTML = countdownText;
     document.getElementById('countdown-specific').innerHTML = countdownText;


     // Update the countdown display every second
     // Use a separate setTimeout to avoid clashing with other scripts
     setTimeout(updateCountdownWidget, 1000);
     setTimeout(updateSpecificCountdown, 1000);
   }
   }


   // Initial call to start the widget countdown
   // Ensure this does not conflict with window.onload used by other scripts
   window.onload = function() {
   if (window.addEventListener) {
     updateCountdownWidget();
    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>