Countdown Timer
by pphheerroonn
HTML
<p>We offer free home delivery. <span id="countdown"></span></p>
<script>
function getDeliveryCountdown() {
// Bank holidays for 2025 (Ireland)
const bankHolidays = [
'2025-01-01', // New Year's Day
'2025-03-17', // St. Patrick's Day
'2025-04-21', // Easter Monday
'2025-05-05', // May Bank Holiday
'2025-06-02', // June Bank Holiday
'2025-08-04', // August Bank Holiday
'2025-10-27', // October Bank Holiday
'2025-12-25', // Christmas Day
'2025-12-26' // St. Stephen's Day
];
const now = new Date();
const cutoffTime = 15; // 3 PM
// Helper function to check if date is weekend
function isWeekend(date) {
const day = date.getDay();
return day === 0 || day === 6; // Sunday = 0, Saturday = 6
}
// Helper function to check if date is bank holiday
function isBankHoliday(date) {
const dateStr = date.toISOString().split('T')[0];
return bankHolidays.includes(dateStr);
}
// Helper function to add days and skip weekends/holidays
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;
}
// Determine shipping deadline
let shippingDate = new Date(now);
// If today is weekend or holiday, find next working day
while (isWeekend(shippingDate) || isBankHoliday(shippingDate)) {
shippingDate.setDate(shippingDate.getDate() + 1);
}
// Set cutoff time for shipping date
let cutoffDateTime = new Date(shippingDate);
cutoffDateTime.setHours(cutoffTime, 0, 0, 0);
// If past cutoff time today, move to next working day
if (now >= cutoffDateTime) {
shippingDate =...
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...