Countdown Timer (Seconds)

by pphheerroonn

HTML

<p>We offer free home delivery. <span id="countdown"></span></p>
<script>
function getDeliveryCountdown(mockCurrentTime = null) {
    // Bank holidays for 2025 (Ireland)
    const bankHolidays = [
        '2025-01-01', '2025-03-17', '2025-04-21', '2025-05-05', '2025-06-02', 
        '2025-08-04', '2025-10-27', '2025-12-25', '2025-12-26'
    ];
    
    const now = mockCurrentTime ? new Date(mockCurrentTime) : new Date();
    const cutoffTime = 16; // 3 PM
    
    function isWeekend(date) {
        const day = date.getDay();
        return day === 0 || day === 6; // Sunday = 0, Saturday = 6
    }
    
    function isBankHoliday(date) {
        const dateStr = date.toISOString().split('T')[0];
        return bankHolidays.includes(dateStr);
    }
    
    function getNextWorkingDay(date) {
        let nextDay = new Date(date);
        nextDay.setDate(nextDay.getDate() + 1);
        while (isWeekend(nextDay) || isBankHoliday(nextDay)) {
            nextDay.setDate(nextDay.getDate() + 1);
        }
        return nextDay;
    }
    
    let shippingDate = new Date(now);
    while (isWeekend(shippingDate) || isBankHoliday(shippingDate)) {
        shippingDate.setDate(shippingDate.getDate() + 1);
    }
    
    let cutoffDateTime = new Date(shippingDate);
    cutoffDateTime.setHours(cutoffTime, 0, 0, 0);
    
    if (now >= cutoffDateTime) {
        shippingDate = getNextWorkingDay(shippingDate);
        cutoffDateTime = new Date(shippingDate);
        cutoffDateTime.setHours(cutoffTime, 0, 0, 0);
    }
    
    const deliveryDate = getNextWorkingDay(shippingDate);
    const timeRemaining = cutoffDateTime - now;
    
    if (timeRemaining <= 0) {
        return "Cutoff time has passed for today";
    }
    
    const hours = Math.floor(timeRemaining / (1000 * 60 * 60));
    const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
    const seconds = Math.floor((timeRemaining % (1000 * 60)) /...

JavaScript

// Test Script for Delivery Countdown Timer
function getDeliveryCountdown(mockCurrentTime = null) {
    // Bank holidays for 2025 (Ireland)
    const bankHolidays = [
        '2025-01-01', '2025-03-17', '2025-04-21', '2025-05-05', '2025-06-02', 
        '2025-08-04', '2025-10-27', '2025-12-25', '2025-12-26'
    ];
    
    const now = mockCurrentTime ? new Date(mockCurrentTime) : new Date();
    const cutoffTime = 15; // 3 PM
    
    function isWeekend(date) {
        const day = date.getDay();
        return day === 0 || day === 6;
    }
    
    function isBankHoliday(date) {
        const dateStr = date.toISOString().split('T')[0];
        return bankHolidays.includes(dateStr);
    }
    
    function getNextWorkingDay(date) {
        let nextDay = new Date(date);
        nextDay.setDate(nextDay.getDate() + 1);
        while (isWeekend(nextDay) || isBankHoliday(nextDay)) {
            nextDay.setDate(nextDay.getDate() + 1);
        }
        return nextDay;
    }
    
    let shippingDate = new Date(now);
    while (isWeekend(shippingDate) || isBankHoliday(shippingDate)) {
        shippingDate.setDate(shippingDate.getDate() + 1);
    }
    
    let cutoffDateTime = new Date(shippingDate);
    cutoffDateTime.setHours(cutoffTime, 0, 0, 0);
    
    if (now >= cutoffDateTime) {
        shippingDate = getNextWorkingDay(shippingDate);
        cutoffDateTime = new Date(shippingDate);
        cutoffDateTime.setHours(cutoffTime, 0, 0, 0);
    }
    
    const deliveryDate = getNextWorkingDay(shippingDate);
    const timeRemaining = cutoffDateTime - now;
    
    if (timeRemaining <= 0) {
        return "Cutoff time has passed for today";
    }
    
    const hours = Math.floor(timeRemaining / (1000 * 60 * 60));
    const minutes = Math.floor((timeRemaining % (1000 * 60 * 60)) / (1000 * 60));
    
    const tomorrow = new Date(now);
    tomorrow.setDate(tomorrow.getDate() + 1);
    
    let deliveryText;
    if...