首页 > 解决方案 > Javascript计算交货日期

问题描述

我有复杂的问题。我有一个网站,显示产品交付的天数。但我需要更改 javascript 不显示天数,而是显示交付的确切日期。

这是现在的样子

图片

这是原始代码的一部分:

function getProductRecordHTML(Product)
{
    var manufacturer = "", article_show = "", name = "";
                
    var time_to_exe = Product.time_to_exe;
    

    if(time_to_exe >= 6)
    {
        time_to_exe = time_to_exe + " days";
    }

这个time_to_exe显示了您可以在图像上看到的天数

由于我有不同的交货天数,这是我需要的结果:

如果订单在下午 12 点前下单

所有产品有 1 天交货,然后将这一天替换为下一个工作日日期

所有2天交货的产品,然后今天替换后天工作日

等等......这一直持续到 5 天交货。

当它显示 6 天交货时,它应该如下所示:

订单需要在周一至周四下午 12 点下单,以便在下周一发货,因此网站上的 6 天替换为下周一日期

Example: If you order on Thursday 19/08/2021 till 12 PM then delivery will be on 23/08/2021, but if you will order on Friday 20/08/21 or any day till next Monday, then delivery will be on 30/08/2021

当它显示 7 天交货时,它应该如下所示:

订单需要在周一至周三下午 12 点下单,以便在下周一发货,因此网站上的 7 天替换为下周一日期

Example: If you order on Wednesday 18/08/2021 till 12 PM then delivery will be on 23/08/2021, but if you will order on Thursday 19/08/21 or any day till next Monday, then delivery will be on 30/08/2021

当它显示 8、9 和 10 天交货时,它应该如下所示:

订单需要从周一到周三下午 12 点进行,以便在一周后的下周一交货,因此网站上的 8、9 和 10 天替换为一周后的下周一日期

Example: If you order on Wednesday 18/08/2021 till 12 PM then delivery will be on 30/08/2021, but if you will order on Thursday 19/08/21 or any day till next Monday, then delivery will be on 06/09/2021

当它显示 15 天和 18 天交货时,它应该如下所示:

订单需要在周一至周三下午 12 点下单,以便在下周一每周一发货,因此网站上的 15 和 18 天替换为 3 周后最近的周一

Example: If you order Wednesday 18/08/2021 till 12 PM then delivery will be on 13/09/2021, but if you will order on Thursday 19/08/21 or any day till next Monday, then delivery will be on 20/09/2021

当它显示 33 天交货时,它应该如下所示:

订单需要在周一至周三下午 12 点下单,以便在 33 天后的周一交货,因此网站上的 33 天替换为从今天起 33 天后的日期。

我知道,这很复杂,但我希望有人理解我并会尽力帮助我。

PS我试图添加这些日期,但我能做的只是将交货天数添加到当前日期,就像这样(这是一种格式,毕竟它应该是这样的):

在此处输入图像描述

标签: javascript

解决方案


要实现显示的日期而不是剩余天数,您必须生成一个新的 Date 对象并向其添加天数。这可以通过 来完成const date = new Date(),之后您可以使用 轻松修改日期对象值date.setDate(date.getDate() + amountOfDaysRemaings)

根据您的示例,您所要做的就是:

    var displayDate;
    if(time_to_exe >= 6)
    {
        const date = new Date();
        date.setDate(date.getDate() + parseInt(time_to_exe));
        displayDate = date.toLocaleDateString();
    }
    if (displayDate) {
      // put displayDate into the cell 
    } else {
      // put `${time_to_exec} days` into the cell 
    }

推荐阅读