I have this bit of code that I found on a blog post and its a reoccurring 24hr countdown timer using http://keith-wood.name/countdown.html. My purpose of using it is as a deadline for next day delivery so when the deadline of 4pm is passed the timer resets. Next day delivery isn't available on weekends so their is a calculation that adjusts the timer if its Friday, Saturday or Sunday.
This JsFiddle shows a demo where it counts down to 4pm every day, except weekends: https://jsfiddle.net/u3t2n9jk/6/
My Problem
I just need to change the deadline of 4pm to 3pm. If I change the var deadline = 16 to 15 (24hrs clock) for some reason it breaks the Friday, Saturday and Sunday calculation. What else do I need to do to get it working correctly?
function nextDay() {
var target = new Date();
var deadline = 16; // Set 4pm/16:00hrs dispatch deadline
if (target.getHours() < deadline || (target.getHours() == 15 && target.getMinutes() == 59 ) ) {
console.log('Before deadline ' + target.getMinutes());
if ( target.getDay() == 5 && target.getHours() == 16 && target.getSeconds() > 1 ) { // For Friday After 4PM
target.setHours(88, 0, 0, 0);
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setHours(64, 0, 0, 0);
}
else if ( target.getDay() == 7 ) { // For Sunday
target.setHours(30, 0, 0, 0);
}
else {
target.setHours(deadline, 0, 0, 0);
}
} else if (target.getHours() > deadline || (target.getHours() == deadline && target.getMinutes() >= 0) ) {
console.log('After deadline ' + target.getMinutes());
if ( target.getDay() == 5 && target.getHours() == 16 && target.getSeconds() > 1 ) { // For Friday After 4PM
target.setDate(target.getDate() + 1);
target.setHours(88, 0, 0, 0);
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setDate(target.getDate() + 1);
target.setHours(64, 0, 0, 0);
}
else if ( target.getDay() == 7 ) { // For Sunday
target.setDate(target.getDate() + 1);
target.setHours(30, 0, 0, 0);
}
else {
target.setDate(target.getDate() + 1);
target.setHours(deadline, 0, 0, 0);
}
}
return target;
}
Ended up re-writing the logic behind it with a little help from a work colleague. Please note you can copy/paste this code to get it working, however you will need the jQuery Countdown plugin too:
function nextDay() {
var target = new Date();
var hours_now = target.getHours();
target.setHours(15);
target.setMinutes(0);
target.setSeconds(0);
// target is 3 today
if ( target.getDay() == 5 && hours_now >= 15 ) { // For Friday After 3PM
target.setDate(target.getDate() + 3); // target is 3 on Monday
}
else if ( target.getDay() == 6 ) { // For Saturday
target.setDate(target.getDate() + 2); // target is 3 on Monday
}
else if ( target.getDay() == 0 ) { // For Sunday
target.setDate(target.getDate() + 1); // target is 3 on Monday
}
else if (hours_now >= 15){ // Any other day and after deadline
target.setDate(target.getDate() + 1); // target is 3 Tomorrow
}
return target;
}
function reset() {
window.location.reload();
}
$('#timer').countdown({
until: nextDay(),
onExpiry: reset,
expiryText: '<div class="end">You have missed todays dispatch.</div>',
labels: ['Years', 'Months', 'Weeks', 'days', 'hrs', 'min', 's'],
labels1: ['Year', 'Month', 'Week', 'day', 'hr', 'min', 's'],
format: 'HMS'
});