我想在shopify描述产品的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>
但是脚本也有问题
答案 0 :(得分:0)
这实际上并不难。 Date 对象提供一个函数 getDay(),该函数返回星期几。 是0到6之间的整数。
您可以这样做:
var days = 4;
etaUnix = new Date(today.getTime() + (60 * 60 * 24 * days * 1000));
console.log(etaUnix.getDay())
switch (etaUnix.getDay()) {
case 0:
//sunday
days++;
break;
case 6:
//saturday
days += 2;
break;
}
etaUnix = new Date(today.getTime() + (60 * 60 * 24 * days * 1000));
// convert the new time to date object and then to a human readable string
etaForHumans = new Date(etaUnix).toDateString();
在处理星期六和星期日并仅将日期添加一两天的切换块中。
顺便说一句-您的代码中有一些错别字。声明末尾需要; ,而不是
答案 1 :(得分:0)
// 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>
答案 2 :(得分:0)
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>
答案 3 :(得分:0)
momentJS。一探究竟。允许您以理智的方式播放日期。具有用于请求nextBusinessDay()之类的插件。
除非您是ace Java编码员,否则不要摆弄日期和原始Javascript。很糟糕,momentJS存在是有充分理由的。吸力少。