在 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 中增加给定日期的详细内容,更多请关注双恒网络其它相关文章!

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!
8. 精力有限,不少源码未能详细测试(解密),不能分辨部分源码是病毒还是误报,所以没有进行任何修改,大家使用前请进行甄别
9.本站默认解压密码为:www.sudo1.com
本站提供的一切软件、教程和内容信息仅限用于学习和研究目的。
不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。
本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容。
如果您喜欢该程序和内容,请支持正版,购买注册,得到更好的正版服务。
我们非常重视版权问题,如有侵权请邮件与我们联系处理。敬请谅解!

云资源网 » 在 JavaScript 中增加给定日期

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
提示下载完但解压或打开不了?
最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。 若排除这种情况,可在对应资源底部留言,或 联络我们.。
你们有qq群吗怎么加入?
当然有的,如果你是帝国cms、易优cms、和pbootcms系统的爱好者你可以加入我们的QQ千人交流群https://www.sudo1.com/page-qun.html。
  • 会员数(个)
  • 12310资源数(个)
  •        
  • 资源(G)
  •        
  • 今日下载
  • 1505稳定运行(天)

提供最优质的资源集合

立即查看 了解详情