您好,欢迎访问一九零五行业门户网

如何在 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 循环进行迭代,直到发现当前毫秒数小于上次日期的毫秒数。
步骤 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>
以上就是如何在 javascript 中将给定两个日期之间存在的所有日期存储在数组中?的详细内容。
其它类似信息

推荐信息