简体   繁体   English

如何在开始日期和结束日期之间创建日期数组?

[英]HOW do I creating an array of dates between a start and end date?

I want to create a list of dates starting from 2014/0/1 to 2020/11/31 (dates are represented in JavaScript). 我想创建一个从2014/0/1到2020/11/31的日期列表(日期用JavaScript表示)。

This is the code 这是代码

   var initialTime = new Date(2014, 0, 1); 
   var endTime = new Date( 2050, 11, 31); 

   var arrTime = [];
   arrTime.push(initialTime);

   if( initialTime < endTime) {

    for( var q = initialTime; q <=  endTime; q.setDate(q.getDate() + 1)) {
          arrTime.push(q);

    }            
 }

 document.querySelector("#Time").innerHTML = arrTime;

This is what the code returns. 这就是代码返回的内容。 It is just a list of " Sun Jan 01 2051 00:00:00 GMT-0500 (EST)." 它只是“ Sun Jan 01 2051 00:00:00 GMT-0500(EST)”的列表。 How do I correct this? 我该如何纠正?

清单图片

When you do q.setTime( ... ) you are modifying the Date object itself. 当您执行q.setTime( ... )您正在修改Date对象本身。 You are pushing the same object into the array at each iteration, hence modifying it modifies the entire array. 您在每次迭代时都将同一对象推入数组,因此对其进行修改会修改整个数组。

If you only want the string representations of the dates only, you can do: 如果仅只希望日期的字符串表示形式,则可以执行以下操作:

 let initialTime = new Date("2018-03-09Z08:00:00") ,endTime = new Date("2018-03-14Z08:00:00") ,arrTime = [] ; for (let q = initialTime; q <= endTime; q.setDate(q.getDate() + 1)) { arrTime.push(q.toString()); } console.log(arrTime); 

Or, if you want to have an array of actual Date instances: 或者,如果您想要一个实际的Date实例数组:

 let initialTime = new Date("2018-03-09Z08:00:00") ,endTime = new Date("2018-03-14Z08:00:00") ,arrTime = [] ,dayMillisec = 24 * 60 * 60 * 1000 ; for (let q = initialTime; q <= endTime; q = new Date(q.getTime() + dayMillisec)) { arrTime.push(q); } console.log(arrTime); 

You are pushing the same memory reference to the array, hence the changes you make affect all of them. 您正在将相同的内存引用推送到该数组,因此您所做的更改会影响所有这些内存引用。

Try: 尝试:

var copiedDate = new Date(q.getTime());
arrTime.push(copiedDate);

This way you are always pushing a new object. 这样,您总是在推动新对象。

first of all you can not compare two dates using == 首先,您不能使用==比较两个日期

second problem is you need to create a new Date object each time you push one to the array ex. 第二个问题是,每次将一个数组推入数组ex时,都需要创建一个新的Date对象。 .push(new Date(q.getTime())

the next problem is you aren't properly adding a day to the last day each time before you push into the array 下一个问题是,每次插入阵列之前,您都没有在最后一天中正确地添加一天

do something like 做类似的事情

pseudo code --- 伪代码-

 var dates = [];
 while( firstDate < secondDate ){
   // this line modifies the original firstDate reference which you want to make the while loop work
   firstDate.setDate(firstDate.getDate() + 1);
   // this pushes a new date , if you were to push firstDate then you will keep updating every item in the array
   dates.push(new Date(firstDate);
 }
var resolution = 1000, // Number of dates to capture between start and end
  results = [],        // will be populated with the for loop
  start = Date.now(),  // Set to whatever you want
  end = start + (1000 * 60 * 60 * 24), // Set to what ever you want
  delta = end - start

for (let i = 0; i < resolution; i++) {
  let t = (delta / resolution) * i
  results.push(new Date(start + t))
}

console.log(results)

live example: https://jsfiddle.net/5ju7ak75/1/ 实时示例: https//jsfiddle.net/5ju7ak75/1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找是开始日期和结束日期之间的日期 - Find is date between array of start and end dates 如何获取开始日期和结束日期之间的日期,结果日期应采用数组格式,使用 javascript - How to get dates that are between Start date and End date and result date should be in an array format, using javascript 获取开始日期和结束日期之间的日期数组 - Getting an array of dates between start date and end date 给定开始日期和结束日期,创建两者之间的日期数组 - Given a start and end date, create an array of the dates between the two 在ExtJS中获取开始/结束日期之间的日期 - Get the dates between start/end date in ExtJS 如何使用名称对数组进行分组,但是使用Javascript获取所有开始日期的最小值和所有结束日期的最大值 - How do I group array by name but taking minimum of all start dates and max of all end dates using Javascript 开始日期在结束日期之后的两个日期之间的差异 - Difference between two dates with start date after end date JavaScript - 获取开始日期和结束日期之间的日期数组 - JavaScript - Get array of dates between start and end dates 在两个日期之间循环。 开始和结束日期(Fetch API) - Loop between two dates. Start and end date(Fetch API) JavaScript - 在两个日期之间循环。 开始和结束日期 - JavaScript - Loop between two dates. Start and end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM