在 JavaScript 中递增日期


在这篇文章中,我们将讨论如何使用 JavaScript 递增给定的日期。首先,我们将分析并理解这其中的含义。给定一个特定日期 x = “05-02-2023”,我们希望向此日期添加 y = 7 天,并打印结果日期“12-02-2023”。作为人类,我们可以手动将 7 天添加到 2 月 5 日并得到结果。但我们需要 JavaScript 为我们以编程方式完成此操作。

有一些技术可以做到这一点,我们将在本文中讨论。

使用 addDays() 函数

addDays 函数接收两个参数,分别是日期和要递增的天数。在下面的代码中,我们将向当前日期添加 7 天,并将递增后的日期打印到控制台。

示例

function addDays(date, days) {
   
   // Function to add Days
   var result = new Date(date);
   result.setDate(result.getDate() + days);
   return result;
}
let date = new Date();
console.log("Current date is "+date);
let incrementedDate = addDays(date, 7);
console.log("Increment date is "+incrementedDate);

输出

Current date is Tue Mar 07 2023 11:38:38 GMT+0530 (India Standard Time)
Increment date is Tue Mar 14 2023 11:38:38 GMT+0530 (India Standard Time)

使用 setDate() 和 getDate() 函数

getDate 函数返回 1 到 31 之间的整数,表示月份中的某一天。然后使用 setDate 函数将变量的值设置为递增后的日期。在这里,我们将向当前日期添加 14 天,并将结果显示在控制台。递增的天数保存在同一个变量“date”中,因此不需要另一个变量。

示例

let date = new Date();
console.log("Current date is "+date);
date.setDate(date.getDate()+14);
console.log("Increment date is "+date);

输出

Current date is Tue Mar 07 2023 11:40:55 GMT+0530 (India Standard Time)
Increment date is Tue Mar 21 2023 11:40:55 GMT+0530 (India Standard Time)

用户自定义函数

在这里,我们将创建自己的函数,而不是使用任何内置函数来递增日期。我们首先从给定的日期中提取月份中的日整数、月份和年份,并将它们分别存储为变量 d、m 和 y。然后我们将天数添加到 d 或 day 变量,然后在返回时将其转换回 Date 格式。目前,此函数在添加超过 1 个月的天数时功能有限,但可以最好地进行修改或避免,因为存在内置函数。

示例

function AddDays(start,days){
   var d=start.getDate();
   var m=start.getMonth()+1;
   
   //getMonth returns the index of the month hence +1 is added
   var y=start.getYear();
   
   //getYear returns the year minus 1900 in the current javascript version, hence 1900 is added to it below
   var newDate=m+"-"+(d+days)+"-"+(y+1900);
   return new Date(newDate);
}
today=new Date();
console.log("The date today is "+today);
console.log("The date after 5 days is "+AddDays(today,5));

输出

The date today is Tue Mar 07 2023 11:43:02 GMT+0530 (India Standard Time)
The date after 5 days is Sun Mar 12 2023 00:00:00 GMT+0530 (India Standard Time)

仅添加工作日

人们经常会在电子商务网站上看到天数的递增,以显示预计送达时间。此送达时间通常在星期日不可用。计算预计送达日期时,必须排除星期日。也可以排除公众假期。

这里将介绍如何向当前日期添加 7 个工作日

示例

function AddWorkingDays(start,days){
  
  // retrieve the index of the start date
   var d=start.getDay();
   var incrementby=days;
   if(d==0){
     
     // 0 stands for Sunday, if current day is a Sunday then add 1 day
      incrementby++;
   }
   if (d + incrementby >= 6) {
      
      //Subtract days in current working week from working days
      var remainingWorkingDays = incrementby - (5 - d);
      
      //Add current working week's weekend
      incrementby += 2;
      if (remainingWorkingDays > 5) {
         
         //Add two days for every working week by finding out how many weeks are included
         incrementby += 2 * Math.floor(remainingWorkingDays / 5);
        
         //Exclude the final weekend if the remainingWorkingDays is a equal to an exact number of weeks
         if (remainingWorkingDays % 5 == 0)
         incrementby -= 2;
      }
   }
   start.setDate(start.getDate() + incrementby);
   return start;
}
var today=new Date();
console.log("Current date is "+today);
console.log("8 working days later would be "+AddWorkingDays(today,8));

输出

Current date is Tue Mar 07 2023 11:45:58 GMT+0530 (India Standard Time)
8 working days later would be Fri Mar 17 2023 11:45:58 GMT+0530 (India Standard Time)

结论

所有上述方法都允许您向给定的日期添加天、月甚至年。添加工作日函数在行业中非常有用,可以被电子商务平台、送货网站等使用。除了这些方法之外,我们还可以使用 Moment.js 库,但这会给程序增加不必要的复杂性。

更新于:2023年3月9日

2K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.