JavaScript 增加给定日期

JavaScript 增加给定日期

在本文中,我们将讨论如何使用JavaScript增加给定日期。首先,我们需要分析和理解这是什么意思。给定一个特定的日期x =“2023年2月5日”,我们希望在这个日期上增加y = 7天,并打印出结果日期,即“2023年2月12日”。作为人类,我们可以手动将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库,但这会给程序增加不必要的复杂性。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程