店铺配送倒计时周末隐藏及工作日差异化配送时间设置需求
Solution for Delivery Countdown with Weekend Hide & Custom Daily Cutoffs
Got it, let's tackle your two requirements one by one and fix up your code!
1. Hide Countdown Entirely on Weekends
First, we need to check if today is Saturday (getDay() returns 6) or Sunday (getDay() returns 0). If it is, we'll set the countdown div to display: none; otherwise, we'll make sure it's visible.
2. Support Different Delivery Cutoffs per Weekday
Instead of a fixed 14:00 cutoff every day, we can create a simple configuration object that defines the cutoff hour for each weekday. This makes it super easy to adjust later if your delivery times change—like setting an earlier cutoff on Fridays, for example.
Modified Full Code
<script> function addZero(i) { if (i < 10) { i = "0" + i; } return i; } setInterval(function () { const countdownEl = document.getElementById('wc_countdowntimer'); const now = new Date(); const dayOfWeek = now.getDay(); // 0 = Sunday, 1 = Monday, ..., 6 = Saturday // --- Handle Weekend Hide --- if (dayOfWeek === 0 || dayOfWeek === 6) { countdownEl.style.display = 'none'; return; // Exit early, no need to calculate countdown } countdownEl.style.display = 'block'; // Show if it's a weekday // --- Configure Custom Daily Cutoff Hours --- // Key: dayOfWeek (0=Sun, 1=Mon, ...,6=Sat), Value: cutoff hour (24h format) const dailyCutoffs = { 1: 14, // Monday cutoff at 14:00 2: 14, // Tuesday 3: 14, // Wednesday 4: 14, // Thursday 5: 12 // Friday cutoff at 12:00 (adjust this as needed) }; const cutoffHour = dailyCutoffs[dayOfWeek]; let missedTodayDelivery = now.getHours() >= cutoffHour ? 1 : 0; // Calculate next delivery date let end = new Date(now.getFullYear(), now.getMonth(), now.getDate() + missedTodayDelivery, cutoffHour, 0, 0, 0); // Handle Friday cutoff rollover to Monday if (dayOfWeek === 5 && now.getHours() >= cutoffHour) { end.setDate(end.getDate() + 3); // Friday after cutoff → skip weekend to Monday } // Calculate time difference const diffMs = end - now; if (diffMs <= 0) { // Fallback for edge cases (shouldn't happen with proper logic) countdownEl.innerHTML = "<i class='fa fa-clock-o'></i> Næste afsendelse starter snart"; return; } // Convert ms to days, hours, mins, secs const days = Math.floor(diffMs / (1000 * 60 * 60 * 24)); const hours = Math.floor((diffMs % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const mins = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60)); const secs = Math.floor((diffMs % (1000 * 60)) / 1000); // Build the display string let result = "<i class='fa fa-clock-o'></i> Vi afsender om "; if (days > 0) result += `${days} dage `; if (hours > 0 || days > 0) result += `${addZero(hours)}:`; result += `${addZero(mins)}:${addZero(secs)}`; countdownEl.innerHTML = result; }, 1000); </script> <div id="wc_countdowntimer"></div>
Key Changes Explained
- Weekend Hide: We check the weekday first and toggle the div's visibility immediately. If it's the weekend, we exit the function so no unnecessary countdown logic runs.
- Custom Cutoffs: The
dailyCutoffsobject lets you set unique cutoff hours for each weekday. Just update the values here if your delivery schedule changes. - Simplified Time Calculation: Instead of manipulating the Date object for time differences, we calculate days/hours/mins/secs directly from the millisecond difference—this is more straightforward and less error-prone.
- Friday Cutoff Handling: Explicitly rolls over to Monday after Friday's cutoff, so you don't end up counting down to a weekend delivery that doesn't exist.
This should solve both of your issues cleanly!
内容的提问来源于stack exchange,提问作者Mathias Beck
相关产品推荐
相关产品推荐

