首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在JavaScript购物中为date对象创建11天后的JavaScript代码

如何在JavaScript购物中为date对象创建11天后的JavaScript代码
EN

Stack Overflow用户
提问于 2019-03-01 08:34:48
回答 3查看 162关注 0票数 2

我想把日期注入到shopify description产品的html中

它会说

我们将从今天起+ 11天内发货.

如果是星期六或者星期天的话就会移到星期一,因为星期六或者星期天是休息日。

代码现在是这样的,但是如果需要的话,不知道如何把它移到第一个星期一

代码语言:javascript
复制
// 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;
代码语言:javascript
复制
<div id="productEta">Product will arrive by: </div>

但是脚本也有问题

EN

回答 3

Stack Overflow用户

发布于 2019-03-01 08:52:09

代码语言:javascript
复制
// 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;
代码语言:javascript
复制
<div id="productEta">Product will arrive by: </div>

票数 0
EN

Stack Overflow用户

发布于 2019-03-01 08:53:03

代码语言:javascript
复制
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;
代码语言:javascript
复制
<div id="productEta">Product will arrive by: </div>

票数 0
EN

Stack Overflow用户

发布于 2019-03-01 13:03:13

momentJS。看看这个。允许你以一种理智的方式处理日期。有用于请求nextBusinessDay()和更多内容的插件。

不要摆弄日期和普通的Javascript,除非你是一个高手Javascript程序员。它很糟糕,momentJS的存在是有充分理由的。更少的差劲。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54936334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档