在两个日期之间创建一个天数数组 [英] Create an array of days between two dates
问题描述
I have 2 dates: StartDate and EndDate
With the help of the following code, i am able to create an array of months between 2 dates.
Result = [10/09/2018, 11/09/2018, 12/09/2018, 01/09/2019...08/09/2019]
How do i calculate the number of days in each of those months and then create an array. What i am trying to do is calculate the number of days between those months.(depending on number of days)
Oct-Nov - 30 days
Nov-Dec - 31 days
Dec- Jan - 30 days
.
.
.
July- Aug - 30 days
and so forth (depending on number of days)
and then create an array from that.
array = [30,30,31,30,28....]
I also have a function that calculates number of days in each month depending on leap year.
<pre lang="text"><pre lang="Javascript">
function isLeapYear(year) {
return (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
}
function getDaysInMonth(year, month) {
return [31, (isLeapYear(year) ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31][month];
}
我们将非常感谢任何帮助。谢谢。
我尝试了什么:
Any help will be greatly appreciated. Thank you.
What I have tried:
var getDateArray = function(start, end) {
var arr = [];
var dt0 = new Date(start);
var endD = new Date(end);
while (dt0 <= endD) {
arr.push(new Date(dt0));
dt0.setMonth(dt0.getMonth() + 1);
}
return arr;
}
var z = "10/09/2018"; var y = "08/08/2019"
var dateArr = getDateArray(z, y);
推荐答案
怎么样这个?
How about this?
function getDateArray (start, end) {
var arr = [];
var startDate = new Date(start);
var endDate = new Date(end);
while (startDate <= endDate) {
arr.push(new Date(startDate.setMonth(startDate.getMonth() + 1)));
}
return arr;
}
function test (){
var z = "2018-09-01"; var y = "2019-09-10"
var dateArr = getDateArray(z, y);
alert(dateArr[0] + "\n" + dateArr[1] + "\n" + dateArr[2]);
}
我将你的dt0和endD改为startDate和endDate。
同时循环有点容易,它似乎工作。
你可以看到它运行于:
JS Bin - 协作式JavaScript调试 [ ^ ]
弹出数组中的前三个日期以显示它的工作原理。
看起来像:
https://i.stack.imgur。 com / n2nn3.png [ ^ ]
之后我添加了daysBetween()函数:
I changed your dt0 and endD to startDate and endDate.
Also the while loop is a bit easier and it seems to work.
You can see it run at:
JS Bin - Collaborative JavaScript Debugging[^]
It pops up the first three dates in the array to show it works.
Looks like:
https://i.stack.imgur.com/n2nn3.png[^]
After that I added the daysBetween() function:
function daysBetween ( date1, date2 )
{
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
return dayDifference = Math.ceil(timeDiff / (1000 * 3600 * 24));
}
然后我在两个日期也调用了test()方法。
Then I make the test() method call that too on two of the dates.
alert(daysBetween(dateArr[0], dateArr[3]))
在jsbin上尝试一下。
你会看到:
https://i.stack.imgur.com/awk1Q.png [ ^ ]
最终代码
Try it out at the jsbin.
You will see :
https://i.stack.imgur.com/awk1Q.png[^]
Final Code
function calculateFinalArray(dateArr){
var dayCounts = "";
for (var x = 0; x < dateArr.length-1;x++)
{
dayCounts += daysBetween(dateArr[x],dateArr[x+1]) + ":";
}
alert(dayCounts);
}
看起来像这样:(分号分隔列表,你可以改变):
< a href =https://i.stack.imgur.com/tSm0H.png> https://i.stack.imgur.com/tSm0H.png [ ^ ]
您可以使用两个日期之间的差异除以一天中的毫秒数。从另一个中减去一个,您可以计算两者之间的天数,如下所示:
You can use the difference between the two dates divided by the number of milliseconds in a day. Subtracting one from the other you can calculate the number of days between the two as follows:
function myFunction() {
var startValue = new Date(2018,9,9,0,0,0,0);
var endValue = new Date(2018,10,9,0,0,0,0);
var days = (endValue - startValue) / (1000 * 60 * 60 * 24);
}
请注意,月份的范围是0到11,而不是1到12.所以上面计算的是10月9日到10月9日之间的天数。 11月9日。
Note that the months are in the range 0 to 11, rather than 1 to 12. So the above calculates the number of days between 9th October and 9th November.
这篇关于在两个日期之间创建一个天数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!