This wiki is still in active development, many areas of the site are unstable or broken.

In the meantime, consider adding content to pages you like!

MediaWiki:Countdown.js

From NBITTRPG Wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
$(function () {
    var daysMap = {
        'sunday': 0, 'sun': 0,
        'monday': 1, 'mon': 1,
        'tuesday': 2, 'tue': 2,
        'wednesday': 3, 'wed': 3,
        'thursday': 4, 'thu': 4,
        'friday': 5, 'fri': 5,
        'saturday': 6, 'sat': 6
    };

    $('.countdown').each(function () {
        var $container = $(this);
        var timeStr = $container.attr('data-time').trim();
        var dayStr = ($container.attr('data-day') || '').trim().toLowerCase();
        var $textSpan = $container.find('.countdown-text');

        var parts = timeStr.split(':');
        var targetHour = parseInt(parts[0], 10) || 0;
        var targetMinute = parseInt(parts[1], 10) || 0;
        var targetSecond = parseInt(parts[2], 10) || 0;

        var targetDay = daysMap.hasOwnProperty(dayStr) ? daysMap[dayStr] : null;

        function updateCountdown() {
            var now = new Date();
            
            // Construct base target date for TODAY in UTC
            var target = new Date(Date.UTC(
                now.getUTCFullYear(),
                now.getUTCMonth(),
                now.getUTCDate(),
                targetHour,
                targetMinute,
                targetSecond
            ));

            if (targetDay !== null) {
                // Weekly mode
                var currentUTCDay = now.getUTCDay();
                var daysUntil = (targetDay - currentUTCDay + 7) % 7;

                // If target day is today but time has passed, jump to next week
                if (daysUntil === 0 && now.getTime() >= target.getTime()) {
                    daysUntil = 7;
                }

                target.setUTCDate(target.getUTCDate() + daysUntil);
            } else {
                // Daily mode: if target time today has passed, roll to tomorrow
                if (now.getTime() >= target.getTime()) {
                    target.setUTCDate(target.getUTCDate() + 1);
                }
            }

            var distance = target.getTime() - now.getTime();

            var days = Math.floor(distance / (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);

            var hStr = hours < 10 ? '0' + hours : hours;
            var mStr = minutes < 10 ? '0' + minutes : minutes;
            var sStr = seconds < 10 ? '0' + seconds : seconds;

            var timerDisplay = '';
            if (days > 0) {
                timerDisplay += days + 'd ';
            }
            timerDisplay += hStr + 'h ' + mStr + 'm ' + sStr + 's';

            $textSpan.text(timerDisplay);
        }

        updateCountdown();
        setInterval(updateCountdown, 1000);
    });
});