0

For my Woocommerce shop I am busy writing the expected shipping date of my products in javascript. Via (among others) w3schools I came after some merging into the code below. This is pretty close to what I'm looking for.

<style>
    
    h1{     
    margin-top: 0px !important;
    margin-bottom: 0px !important;
    float: left !important;
    padding-right: 10px !important;
    }
    
</style>
        
        
<h1 id="dag"></h1><h1 id="datum"></h1><h1 id="maand"></h1>
        
<script>
        var d = new Date();
        d.setDate(d.getDate() + 3);
        var days = ["Zondag","Maandag","Dinsdag","Woensdag","Donderdag","Vrijdag","Zaterdag"];
        document.getElementById("dag").innerHTML = days[d.getDay()];
        
        var d = new Date();
        d.setDate(d.getDate() + 3);
        document.getElementById("datum").innerHTML = d.getDate();
        
        var d = new Date();
        d.setDate(d.getDate() + 3);
        var months = ["Januari","Februari","Maart","April","Mei","Juni","Juli","Augustus","September","Oktober","November","December"];
        document.getElementById("maand").innerHTML = months[d.getMonth()];
        
        
</script>

However, I would add one last thing. No products will be shipped on weekends (Saturday and Sunday). So it is the intention that the 'Saturday' and 'Sunday' are excluded. If Saturday or Sunday, the next working day (Monday) must be shown.

Example: Saturday January 9 = Monday January 11 Sunday January 10 = Monday January 11

Therefore, needed help as i lacked that knowledge :(

EDIT:

How to add this to my wordpress pages (via Code Snippet)?? See snippet screenshot

Martijn
  • 15
  • 2
  • Does this answer your question? [Calculate working days between two dates in Javascript excepts holidays](https://stackoverflow.com/questions/37069186/calculate-working-days-between-two-dates-in-javascript-excepts-holidays) – Will Dec 29 '20 at 21:07
  • It seems orders are shipped 3 days after ordering, except where the 3 days ends on a Sat or Sun. So for orders placed on Wed add 5 days and for orders placed on Thu add 4 days. Otherwise, add 3: `d.setDate(d.getDate() + (d.getDate() == 3? 5: d.getDate() == 4? 4 : 3))`. – RobG Dec 30 '20 at 01:25

1 Answers1

0

A simple check using getDay() might work for you. You are already using getDay() in your arrays to check which day it is so we can expand upon this and add some simple checks that would change your d date variable if the result of d.getDay() is 0 (Sunday/Zondag) or 6 (Saturday/Zaterdag). Assuming the d.setDate(d.getDate() + 3); block of your code represents 3-day shipping, we can first add the 3 days of shipping time to the date, then check which day the resulting d date variable is and, if it is a Saturday, add an extra 2 days of shipping to the d date variable. If the resulting d date variable is a Sunday after adding the 3 days of shipping, we would just need to add 1 additional day of shipping.

Also, there is no need to create 3 instances of the current date as you can solve this issue with just 1 instance of the date.

Here is the solution:

let days = ["Zondag","Maandag","Dinsdag","Woensdag","Donderdag","Vrijdag","Zaterdag"];
let months = ["Januari","Februari","Maart","April","Mei","Juni","Juli","Augustus","September","Oktober","November","December"];
let d = new Date();

d.setDate(d.getDate() + 3);
if (d.getDay() === 0)
    d.setDate(d.getDate() + 1)
else if (d.getDay() === 6)
    d.setDate(d.getDate() + 2)

document.getElementById("dag").innerHTML = days[d.getDay()];
document.getElementById("datum").innerHTML = d.getDate();
document.getElementById("maand").innerHTML = months[d.getMonth()];
Chazaster
  • 30
  • 7
  • Thanks, thats what i'm looking for. One last question, do you know how to add this code to functions.php file? Does some sort of function need to be built around it? – Martijn Jan 02 '21 at 12:54