如何在 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 之间的所有日期。

Open Compiler
<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 循环和毫秒数来实现上述算法以获取两个日期之间的所有日期。

Open Compiler
<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>

Learn JavaScript in-depth with real-world projects through our JavaScript certification course. Enroll and become a certified expert to boost your career.

使用 momentJS 库

momentJS 库允许我们操作日期。

语法

用户可以按照以下语法使用 momentJS 库获取两个日期之间的所有日期。

while (currentDate.add(1, "days").diff(lastDate) < 0) { 
   allDates.push(currentDate.clone().toDate());
}

在上述语法中,我们使用了 momentJS 库的 add() 和 diff() 方法。

示例 3

在下面的示例中,我们从用户那里获取开始日期和结束日期。之后,我们使用了输入日期并使用 momentJS 库创建了日期。

接下来,我们使用 add() 方法将一天添加到当前日期。此外,我们还使用 diff() 方法获取当前日期和结束日期之间的差值。

Open Compiler
<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>

更新于: 2023年2月16日

3K+ 浏览量

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告