Skip to content

Instantly share code, notes, and snippets.

@ihorduchenko
Created May 16, 2024 21:31
Show Gist options
  • Save ihorduchenko/ba6d44ae0000ea6e384eaa4ef1a3336d to your computer and use it in GitHub Desktop.
Save ihorduchenko/ba6d44ae0000ea6e384eaa4ef1a3336d to your computer and use it in GitHub Desktop.
Calculate estimated shipping date based on predefined interval, excluding weekends and holidays
<script>
// Calculate the sequence number of the holidays here: https://miniwebtool.com/day-of-the-year-calculator/
// function to check leap year
function checkLeapYear(year) {
const isLeap = new Date(year, 1, 29).getDate() === 29;
return isLeap;
}
const currentYear = new Date().getFullYear();
if (checkLeapYear(currentYear)) {
const PUBLIC_HOLIDAYS_DAYS = [1,97,100,122,140,150,277,317,360,361];
} else {
const PUBLIC_HOLIDAYS_DAYS = [1,97,100,121,139,149,276,316,359,360];
}
const dateFormatConnector = '{{ "products.product.date_format_connector" | t }}';
function calcWithHolidays(holidays, today, total) {
var finalTotal = total;
var futureDate = today + total;
for (let i = today; i <= futureDate; i++) {
if (holidays.includes(i)) {
finalTotal++;
}
}
return finalTotal
}
function getShortDayName(day) {
const dayNumber = day.getDay();
const shortDayNames = ['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'];
return shortDayNames[dayNumber];
}
function getFullDayName(day) {
const dayNumber = day.getDay();
const longDayNames = ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Sonnabend'];
return longDayNames[dayNumber];
}
function getShortMonthName(day) {
const monthNumber = day.getMonth();
const shortMonthNames = ['Jan', 'Feb', 'Mär', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'];
return shortMonthNames[monthNumber];
}
function getFullMonthName(day) {
const monthNumber = day.getMonth();
const shortMonthNames = ['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'];
return shortMonthNames[monthNumber];
}
function calculateDate() {
// define which class to alter
const className = "dynamic-date";
// start with today
const today = new Date();
var hours = today.getHours();
var extraDay = hours < 12 ? 0 : 1;
var start = new Date(today.getFullYear(), 0, 0);
var diff = today - start;
var oneDay = 1000 * 60 * 60 * 24;
var day = Math.floor(diff / oneDay);
console.log(day);
console.log(hours);
const currentDayOfWeekNumber = today.getDay();
// skip over weekend logic
// Monday -> Tuesday, Tuesday -> Wednesday, Wedneday -> Friday
let futureTotalDays = 2;
let calcFutureTotalDays;
// Thursday -> Monday
if (currentDayOfWeekNumber === 4 ) {
futureTotalDays = 4;
calcFutureTotalDays = calcWithHolidays(PUBLIC_HOLIDAYS_DAYS, day, futureTotalDays);
futureTotalDays = Math.max(futureTotalDays, calcFutureTotalDays);
}
// Friday -> Monday
else if (currentDayOfWeekNumber === 5 ) {
futureTotalDays = 3;
calcFutureTotalDays = calcWithHolidays(PUBLIC_HOLIDAYS_DAYS, day, futureTotalDays);
futureTotalDays = Math.max(futureTotalDays, calcFutureTotalDays);
}
// Saturday -> Monday
else if (currentDayOfWeekNumber === 6 ) {
futureTotalDays = 2;
calcFutureTotalDays = calcWithHolidays(PUBLIC_HOLIDAYS_DAYS, day, futureTotalDays);
futureTotalDays = Math.max(futureTotalDays, calcFutureTotalDays);
}
// Saturday -> Tuesday
else if (currentDayOfWeekNumber === 0 ) {
futureTotalDays = 1;
calcFutureTotalDays = calcWithHolidays(PUBLIC_HOLIDAYS_DAYS, day, futureTotalDays);
futureTotalDays = Math.max(futureTotalDays, calcFutureTotalDays);
}
// Calculate Future Delivery Date
const futureDate = new Date(today.getTime() + ((futureTotalDays + extraDay) * 24 * 60 * 60 * 1000));
// Get the day, month, and year of the Future Delivery day
const shortDayName = getShortDayName(futureDate);
const longDayName = getFullDayName(futureDate);
const month = getFullMonthName(futureDate);
const date = futureDate.getDate();
let content = `${longDayName}${dateFormatConnector}${date}. ${month}`;
// get the element with the "date" id
var dateElements = Array.from(document.querySelectorAll("." + className));
for (const dateElement of dateElements) {
// set the text of the paragraph element to the day and date
dateElement.textContent = content
}
console.log("Set the content of " + dateElements.length + " elements with class " + className + " to", content)
}
setInterval(() => {
calculateDate();
}, 100)
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment