javascript 将日期对象存储到数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18943252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
store date objects to array
提问by Thommy
I have two dates and want to save the days in between (plus start and end date) in an array
我有两个日期,想在数组中保存两者之间的天数(加上开始日期和结束日期)
var date1 = new Date("Sep 23, 2013 12:00:00"); // Monday
var date2 = new Date("Sep 26, 2013 12:00:00"); // Thursday
var alldates = [];
for (var i=0; date1 <= date2; date1.setDate(date1.getDate() + 1), i++) {
alldates.push(date1);
}
alert(alldates.join('\n'));
With this code alert(alldates.join('\n'));
shows the following
使用此代码alert(alldates.join('\n'));
显示以下内容
Fri Sep 27 2013 12:00:00 GMT+0200
Fri Sep 27 2013 12:00:00 GMT+0200
Fri Sep 27 2013 12:00:00 GMT+0200
Fri Sep 27 2013 12:00:00 GMT+0200
I am new to Javascript and want to get further understanding, so thank you for any explanation why the alert
does not show
我是 Javascript 的新手,想进一步了解,所以感谢您对为什么alert
不显示的任何解释
Mon Sep 23 2013 12:00:00 GMT+0200
Tue Sep 24 2013 12:00:00 GMT+0200
Wed Sep 25 2013 12:00:00 GMT+0200
Thu Sep 26 2013 12:00:00 GMT+0200
回答by jaudette
The problem you have is that you push references to the date1
object. When you change the date on it in your loop, you update the object, but all references still point to the same object.
您遇到的问题是您推送对date1
对象的引用。当您在循环中更改日期时,您会更新对象,但所有引用仍指向同一个对象。
You need to either push a text representation of your date, or a copy of your date1
object
您需要推送日期的文本表示,或date1
对象的副本
for (var i=0; date1 <= date2; date1.setDate(date1.getDate() + 1), i++) {
alldates.push(new Date(date1));
}
alert(alldates.join('\n'));
As suggested, with a while loop
按照建议,使用 while 循环
while( date1 <= date2 ) {
alldates.push(new Date(date1));
date1.setDate( date1.getDate() +1 );
}
回答by Kelsadita
Your array is storing the references for the single date object and everytime when setDate is called each of them are getting updated with new date value.
您的数组正在存储单个日期对象的引用,并且每次调用 setDate 时,每个对象都会使用新的日期值进行更新。
So it will be better to push the new date object in array like this,
所以最好像这样在数组中推送新的日期对象,
var date1 = new Date("Sep 23, 2013 12:00:00"); // Monday
var date2 = new Date("Sep 26, 2013 12:00:00"); // Thursday
var alldates = [];
// pushing first date
alldates.push(new Date(date1.setDate(date1.getDate())));
for (var i=0; date1 <= date2; i++) {
alldates.push(new Date(date1.setDate(date1.getDate() + 1)));
}
alert(alldates.join('\n'));
回答by NWRichmond
To echo the other answers, the issue is that the element being pushed to the array isn't a value which stays the same - it refers to a Date
object which changes in the loop, so all references to date1
stored in alldates
are set to the final value of date1
.
为了回应其他答案,问题在于被推送到数组的元素不是一个保持不变的值——它指的是一个Date
在循环中发生变化的对象,因此所有对date1
存储的引用alldates
都设置为最终值的date1
。
The accepted answer does the job, but it also changes the value of date1
. The OP set up the code this way, which indicates that this isn't a problem for them. But if you'd prefer to not mutate date1
, here's an alternative approach:
接受的答案可以完成工作,但它也会改变 的值date1
。OP 以这种方式设置代码,这表明这对他们来说不是问题。但如果您不想 mutate date1
,这里有另一种方法:
const date1 = new Date("Sep 23, 2013 12:00:00"); // Monday
const date2 = new Date("Sep 26, 2013 12:00:00"); // Thursday
const alldates = Array
.from(Array(date2.getDate() - date1.getDate() + 1), () => new Date())
.map((el, ind) => new Date(el.setTime(date1.getTime() + 1000 * 60 * 60 * 24 * ind)));
console.log(alldates.join("\n"));
EXPLANATION: This first creates an array of sufficient size to store the start date, end date, and days in between, and populates the array with Dateobjects. Next, map
applies a function to each element in the array, and this function uses the index of each successive value multiplied by the number of milliseconds in a day to produce the desired result.
解释:这首先创建一个足够大小的数组来存储开始日期、结束日期和其间的天数,并用Date对象填充该数组。接下来,map
对数组中的每个元素应用一个函数,该函数使用每个连续值的索引乘以一天中的毫秒数来产生所需的结果。