在 JavaScript 中增加给定日期

PHPz
PHPz 转载
2023-09-10 13:17:02 731浏览

在 JavaScript 中增加给定日期

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

我们将在本文中讨论一些技术来做到这一点。

使用 addDays() 函数

addDays 函数接受 2 个参数,分别是日期和要递增的天数。在下面的代码中,我们将在当前日期上添加 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 之间的整数,表示该月中的第几天。然后使用设置日期函数将变量的值设置为递增日期。在这里,我们将在当前日期上添加 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 变量,然后在返回时将其转换回日期格式。目前,此函数在添加超过 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库,但这会给工作带来不必要的复杂性。程序。

以上就是在 JavaScript 中增加给定日期的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除