I want to inject date into html in shopify description product
That it would say
We will ship the product from today + 11 days…..
And if it falls to saturday or sunday, it will be moved to monday cause we are closed on saturday or sunday
code is this for now, but dunno how to move it to first monday if necessary
// get the destination within the DOM
var wrapper = document.getElementById('productEta'),
// get today as a js Date object
today = new Date(),
// get the Unix of today (miliseconds) and add desired time (3 weeks)
etaUnix = today.getTime() + (60 * 60 * 24 * 11 * 1000),
// convert the new time to date object and then to a human readable string
etaForHumans = new Date(etaUnix).toDateString();
// set the destination inner html to be what it already is
// plus a space and the human readable string.
wrapper.innerHTML += ' ' + etaForHumans;
<div id="productEta">Product will arrive by: </div>
But something is wrong with the script also
4
Answers
This actually ain’t too hard.
The Date object provides a function getDay() which returns the day of the week.
It’s an integer between 0 and 6.
You can do it like this:
In the switch block where handling saturdays and sundays and simply adding one or two days to the date.
By the way – there are some typos in your code. There needs to be a ; at the end of a statement not a ,
momentJS. Check it out. Allows you play with dates in a sane way. Has plugins for things like asking for the nextBusinessDay() and much more.
Don’t fiddle with dates and vanilla Javascript unless you are an ace Javascript coder. It sucks and momentJS exists for good reason. Less suckage.