Skip to content

Instantly share code, notes, and snippets.

@dylanjhunt
Last active April 28, 2021 15:31
Show Gist options
  • Save dylanjhunt/8e132a2061e25cea2ba34ee5c7aeffdd to your computer and use it in GitHub Desktop.
Save dylanjhunt/8e132a2061e25cea2ba34ee5c7aeffdd to your computer and use it in GitHub Desktop.
How to make a delivery date estimator that avoids weekends for a Shopify product page
$(document).ready(function() {
function calculateHMSleft() {
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var monthday = new Array(12);
monthday[0] = "January";
monthday[1] = "February";
monthday[2] = "March";
monthday[3] = "April";
monthday[4] = "May";
monthday[5] = "June";
monthday[6] = "July";
monthday[7] = "August";
monthday[8] = "September";
monthday[9] = "October";
monthday[10] = "November";
monthday[11] = "December";
//calculate
var now = new Date();
var hoursleft = 12-now.getHours();
console.log(hoursleft);
var minutesleft = 59-now.getMinutes();
var secondsleft = 59-now.getSeconds();
//format 0 prefixes
if(minutesleft<10) minutesleft = "0"+minutesleft;
if(secondsleft<10) secondsleft = "0"+secondsleft;
if (hoursleft < 0) {
var hoursleft = hoursleft + 24
}
//display
$('#HMSremaining').html(hoursleft+" hours and "+minutesleft+" minutes");
if (hoursleft > 12) {
if (d.getDay() == 0 || d.getDay() <= 2) {
d.setHours(d.getHours() + 72);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
document.getElementById("deliveryday").innerHTML = deliver + ' ' + month + ' ' + day;
} else if (d.getDay() == 3) {
d.setHours(d.getHours() + 120);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
document.getElementById("deliveryday").innerHTML = deliver + ' ' + month + ' ' + day;
} else {
d.setHours(d.getHours() + 96);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
document.getElementById("deliveryday").innerHTML = deliver + ' ' + month + '-' + day;
}
} else {
if (d.getDay() > 0 || d.getDay() <= 3) {
d.setHours(d.getHours() + 48);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
document.getElementById("deliveryday").innerHTML = deliver + ' ' + month + ' ' + day;
} else if (d.getDay() == 0) {
d.setHours(d.getHours() + 48);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
document.getElementById("deliveryday").innerHTML = deliver + ' ' + month + ' ' + day;
} else {
d.setHours(d.getHours() + 96);
var deliver = weekday[d.getDay()];
var day = d.getDate();
var month = monthday[d.getMonth()];
document.getElementById("deliveryday").innerHTML = deliver + ' ' + month + '-' + day;
}
}
}
calculateHMSleft();
setInterval(calculateHMSleft, 60000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment