New to this forum. I’ve been racking my brains over this for quite some time now and simply cannot get it working as I would like.
Basically, I’ve created a countdown timer which I want to reset every day at 2pm and deliver the message as below, “Order with ‘countdown’ timer to receive by ‘date'”
So where it is falling over is I cannot seem to get it to skip weekends or UK Bank Holidays and with a UK Bank Holiday this coming Monday, it would be really nice if I could get this fully tested and working.
So if someone orders today and up until 2pm tomorrow, Thursday 2nd May they will receive their item on Friday 3rd May 2024. This is working fine!
But when the time is UK 2pm GMT on Friday 3rd May, it should say “receive by Tuesday 7th May 2024”. If it WAS NOT a UK Bank Holiday this weekend, it should say “receive by Monday 6th May”.
On weekends, it should ideally count down to 2pm this Saturday and tell the customer “receive by Tuesday 7th May”. The same goes for Sunday, 5th and Monday 6th providing they order before 2pm!
On any other weekend which is not followed by a Bank Holiday, it should tell the customer this:-
- Before 2pm on Friday, 10th May, “receive by Monday 13th May.”
- After 2pm on Friday, 10th May, “receive by Tuesday 14th May.”
- After 2pm on Saturday, 11th May, “receive by Tuesday 14th May.”
- After 2pm on Sunday, 12th May, “receive by Tuesday 14th May.”
- BEFORE 2pm on Monday, 13th May, “receive by Tuesday 14th May.”
- After 2pm on Monday, 13th May, “receive by Wednesday 15th May.”
- BEFORE 2pm on Tuesay, 14th May, “receive by Wednesday 15th May.”
and so on.
Any pointers or super genii out there that can help me tweak the code as I have seemingly tried absolutely everything.
I won’t say I think it’s impossible because I believe nothing is impossible. It’s just beyond my skillset!!!
< script >
// UK Bank Holidays between now and 2027
var bankHolidays = ["2024-05-06", "2024-05-27", "2024-08-26", "2024-12-25", "2024-12-26", "2025-01-01", "2025-04-18", "2025-04-21", "2025-05-05", "2025-05-26", "2025-08-25", "2025-12-25", "2025-12-26", "2026-01-01", "2026-04-03", "2026-04-06", "2026-05-04", "2026-05-25", "2026-08-31", "2026-12-25", "2026-12-28"];
// Get today's date and time
var now = new Date();
// Set the countdown date and time to 2 PM today
var countDownDate = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 14, 0, 0, 0);
// If it's past 2 PM, set the countdown date and time to 2 PM tomorrow
if (now > countDownDate) {
countDownDate.setDate(countDownDate.getDate() + 1);
}
// Calculate the delivery day
var deliveryDay = new Date(countDownDate);
// If the order is placed up until 2 PM on Wednesday, the delivery day is Friday
// If the order is placed after 2 PM on Wednesday, the delivery day is Monday
if (deliveryDay.getDay() <= 4) {
deliveryDay.setDate(deliveryDay.getDate() + (5 - deliveryDay.getDay()));
} else {
deliveryDay.setDate(deliveryDay.getDate() + ((8 - deliveryDay.getDay()) % 7));
}
// If the delivery day is a bank holiday, postpone it to the next day
while (bankHolidays.includes(deliveryDay.toISOString().split('T')[0])) {
deliveryDay.setDate(deliveryDay.getDate() + 1);
}
// Update the delivery day element
document.getElementById("deliveryDay").innerHTML = deliveryDay.toDateString();
// Update the countdown timer every 1 second
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the countdown date
var distance = countDownDate - now;
// Time calculations for hours, minutes and seconds
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);
// Output the result in the timer element
document.getElementById("timer").innerHTML = hours + "h " + minutes + "m " + seconds + "s ";
// If the countdown is over, reset the countdown date and time to 2 PM tomorrow and recalculate the delivery day
if (distance < 0) {
countDownDate.setDate(countDownDate.getDate() + 1);
if (countDownDate.getDay() <= 3) {
deliveryDay.setDate(deliveryDay.getDate() + (5 - deliveryDay.getDay()));
} else {
deliveryDay.setDate(deliveryDay.getDate() + ((8 - deliveryDay.getDay()) % 7));
}
while (bankHolidays.includes(deliveryDay.toISOString().split('T')[0])) {
deliveryDay.setDate(deliveryDay.getDate() + 1);
}
document.getElementById("deliveryDay").innerHTML = deliveryDay.toDateString();
}
}, 1000); <
/script>
<div>
<h3>Order within <span class="countdown" id="timer"></span> to receive *<span class="deliverydate" id="deliveryDay"></span></h3>
</div>
<div>
<h4>*Exclusions apply. Click <a href="/despatch-delivery-information" id="deliverydaysmall">HERE</a> for full details or if your item is time critical.</h4>
</div>
<script src="https://bp215.bluepark.build/user/countdowntest.js"