如何在 JavaScript 中儲存兩個給定日期之間所有日期的陣列?
有時,我們需要獲取給定日期範圍內的所有日期。在本教程中,我們將獲取兩個日期並查詢這兩個日期之間的所有日期。此外,我們還將所有日期儲存在陣列中。
在這裡,我們將學習三種在 JavaScript 中儲存兩個給定日期之間所有日期的陣列的方法。
使用 while 迴圈和 setDate() 方法
我們可以使用 while 迴圈進行迭代,並使用 setDate() 方法在日期物件中設定日期。在 while 迴圈的每次迭代中,我們可以將日期增加一天並將其設定為 date1。
語法
使用者可以按照以下語法使用 while 迴圈和 setDate() 方法獲取兩個日期之間的所有日期。
while (date1 <= date2) { dateArray.push(new Date(date1)); date1.setDate(date1.getDate() + 1); }
在上述語法中,date1 是開始日期,date2 是結束日期。
演算法
步驟 1 – 建立兩個日期。
步驟 2 – 使用 while 迴圈,並檢查 date1 是否小於 date2。
步驟 3 – 從 date1 建立一個新日期,並將其推入 dateArray。
步驟 4 – 使用 getDate() 方法獲取 date1 的日期,並加 1。
步驟 5 – 使用 setDate() 方法設定新日期。
示例 1
在下面的示例中,我們使用 Date 物件建立了 date1 和 date2。之後,我們實現了上述演算法來獲取兩個日期之間的所有日期。在輸出中,使用者可以觀察到 date1 和 date2 之間的所有日期。
<html> <body> <h2>Using the <i> setDate() method and while loop</i> to get all dates between two dates in the array format. </h2> <div id = "output"></div> <script> var output = document.getElementById('output'); var date1 = new Date("2023-01-01"); var date2 = new Date("2023-01-11"); output.innerHTML += "The date1 is " + date1 + "<br/>"; output.innerHTML += "The date2 is " + date2 + "<br/>"; var dateArray = []; while (date1 <= date2) { dateArray.push(new Date(date1)); date1.setDate(date1.getDate() + 1); } output.innerHTML += "The date array is <br/>"; for (let i = 0; i < dateArray.length; i++) { output.innerHTML += dateArray[i] + " <br/>"; } </script> </body> </html>
使用 for 迴圈和日期的總毫秒數
在這種方法中,我們將獲取第一個和第二個日期的總毫秒數。之後,我們將持續將 1 天的毫秒數新增到當前日期的總毫秒數中,並使用新的毫秒數,我們可以建立一個日期。
這樣,我們就可以找到給定兩個日期之間的所有日期並將它們儲存在陣列中。
語法
使用者可以按照以下語法使用 for 迴圈和日期的總毫秒數獲取兩個日期之間的所有日期。
for (var currentMillis = startMillis; currentMillis < lastMillis; currentMillis += milliOf1Day) { // pushing updated date to the array dateArray.push(new Date(currentMillis)); }
在上述語法中,milliOf1Day 是一天的總毫秒數。
演算法
步驟 1 – 獲取當前和最後日期的總毫秒數。
步驟 2 – 使用 for 迴圈,並使用開始日期的總毫秒數初始化 currentMillis 變數。
步驟 3 – 使用 for 迴圈迭代,直到我們找到 currentMillis 小於最後日期的毫秒數。
步驟 4 – 同時,將 1 天的毫秒數新增到 currentMillis。
步驟 5 – 使用 currentMillis 建立一個新日期,並在 for 迴圈中將其推入 dateArray 變數。
示例 2
在這個示例中,我們有一個 milliOf1Day 變數,其中我們儲存了 1 天的總毫秒數。之後,我們使用了 for 迴圈和毫秒數來實現上述演算法以獲取兩個日期之間的所有日期。
<html> <body> <h2>Using the <i> setDate() method and while loop </i> to get all dates between two dates in the array format. </h2> <div id = "output"></div> <script> var output = document.getElementById('output'); var firstDate = new Date("2022-11-01"); var secondDate = new Date("2022-11-07"); function getArrayOfDates(firstDate, secondDate) { // calculate milli seconds of 1 day var milliOf1Day = 24 * 60 * 60 * 1000; // calculate the total milliseconds of the start and end date let startMillis = firstDate * 1; let lastMillis = secondDate * 1; var dateArray = []; // In the for-loop, on every iteration, add the total milli seconds of 1 day to current milliseconds, and create a new date for (var currentMillis = startMillis; currentMillis < lastMillis; currentMillis += milliOf1Day) { // pushing updated date to the array dateArray.push(new Date(currentMillis)); } return dateArray; } let dates = getArrayOfDates(firstDate, secondDate) output.innerHTML += "The firstDate is " + firstDate + "<br/>"; output.innerHTML += "The secondDate is " + secondDate + "<br/>"; output.innerHTML += "The date array is <br/>"; // printing the date array for (let i = 0; i < dates.length; i++) { output.innerHTML += dates[i] + " <br/>"; } </script> </body> </html>
使用 momentJS 庫
momentJS 庫允許我們操作日期。
語法
使用者可以按照以下語法使用 momentJS 庫獲取兩個日期之間的所有日期。
while (currentDate.add(1, "days").diff(lastDate) < 0) { allDates.push(currentDate.clone().toDate()); }
在上述語法中,我們使用了 momentJS 庫的 add() 和 diff() 方法。
示例 3
在下面的示例中,我們從使用者那裡獲取開始日期和結束日期。之後,我們使用了輸入日期並使用 momentJS 庫建立了日期。
接下來,我們使用 add() 方法將一天新增到當前日期。此外,我們還使用 diff() 方法獲取當前日期和結束日期之間的差值。
<html> <head> <script src ="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script> <script src = "https://cdnjs.cloudflare.com/ajax/libs/momentrange/4.0.1/moment-range.js"> </script> </head> <body> <h2>Using the <i> setDate() method and while loop </i> to get all dates between two dates in the array format. </h2> <div id="output"> </div> <button onclick="getArrayOfDates()"> Get array of Dates</button> <script> var output = document.getElementById('output'); function getArrayOfDates() { let allDates = []; let startDate = prompt("Enter start date in the MM / DD / YYYY format", "09/23/2022"); let endDate = prompt("Enter end date in the MM / DD / YYYY format ", "10/23/2022"); // create a new date from the custom input let currentDate = moment.utc(new Date(startDate)).startOf("day"); let lastDate = moment.utc(new Date(endDate)).startOf("day"); // add one day to the current date and check the difference between the current date and the last date while (currentDate.add(1, "days").diff(lastDate) < 0) { allDates.push(currentDate.clone().toDate()); } allDates.push(currentDate.clone().toDate()); output.innerHTML += "The currentDate is " + currentDate + "<br/>"; output.innerHTML += "The lastDate is " + lastDate + "<br/>"; output.innerHTML += "The date array is <br/>"; for (let i = 0; i < allDates.length; i++) { output.innerHTML += allDates[i] + " <br/>"; } } </script> </body> </html>