在 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 庫,但這會給程式增加不必要的複雜性。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP