首頁  >  文章  >  web前端  >  在 JavaScript 中增加給定日期

在 JavaScript 中增加給定日期

PHPz
PHPz轉載
2023-09-10 13:17:021114瀏覽

在 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.com。如有侵權,請聯絡admin@php.cn刪除