我想把日期注入到shopify description产品的html中
它会说
我们将从今天起+ 11天内发货.
如果是星期六或者星期天的话就会移到星期一,因为星期六或者星期天是休息日。
代码现在是这样的,但是如果需要的话,不知道如何把它移到第一个星期一
// 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>
但是脚本也有问题
发布于 2019-03-01 00:52:09
// 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);
var day = etaForHumans.getDay()
if(day==0)
etaForHumans.setDate(etaForHumans.getDate() + 1);
if(day==6)
etaForHumans.setDate(etaForHumans.getDate() + 2);
// set the destination inner html to be what it already is
// plus a space and the human readable string.
wrapper.innerHTML += ' ' + etaForHumans+ ' ' + day;
<div id="productEta">Product will arrive by: </div>
发布于 2019-03-01 00:53:03
var wrapper = document.getElementById('productEta');
var today = new Date(Date.parse('2019-03-05'));
var etaDate = new Date(today.getTime() + (60 * 60 * 24 * 11 * 1000))
while(etaDate.getDay() == 0 || etaDate.getDay() == 6){
//Add one day until it is not saturday or sunday
etaDate.setTime(etaDate.getTime() + (60 * 60 * 24 * 1000));
}
var etaForHumans = etaDate.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>
发布于 2019-03-01 05:03:13
momentJS。看看这个。允许你以一种理智的方式处理日期。有用于请求nextBusinessDay()和更多内容的插件。
不要摆弄日期和普通的Javascript,除非你是一个高手Javascript程序员。它很糟糕,momentJS的存在是有充分理由的。更少的差劲。
https://stackoverflow.com/questions/54936334
复制相似问题