
有时,我们需要获取给定日期范围内的所有日期。在本教程中,我们将获取两个日期并查找两个日期之间的所有日期。此外,我们会将所有日期存储在数组中。
在这里,我们将学习三种方法将所有日期存储在 JavaScript 中给定两个日期之间的数组中。
我们可以使用 while 循环进行迭代,并使用 setDate() 方法来设置日期对象中的日期。在 while 循环的每次迭代中,我们可以将日期增加一天并将其设置为 date1。
用户可以按照下面的语法使用 while 循环和 setDate() 方法来获取两个日期之间的所有日期。
立即学习“Java免费学习笔记(深入)”;
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() 方法设置新日期。
在下面的示例中,我们使用 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>
在这种方法中,我们将获得第一个和第二个日期的总毫秒数。之后,我们将继续将 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 变量。
在此示例中,我们有 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库来获取两个日期之间的所有日期。
while (currentDate.add(1, "days").diff(lastDate) < 0) {
allDates.push(currentDate.clone().toDate());
}
在上面的语法中,我们使用了 momentJS 库的 add() 和 diff() 方法。
在下面的示例中,我们从用户处获取开始日期和最后日期。之后,我们使用输入日期并使用 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 中将给定两个日期之间存在的所有日期存储在数组中?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号