在时间戳中创建一个日期数组,递增1年

时间:2016-12-12 21:06:18

标签: javascript arrays datetime

我可以说我将最小日期作为1420070400000 2015-01-01 00:00:00,最长日期为1575158400000,即2019-12-01 00:00:00

然后我尝试在这两个日期之间为每个月创建一个时间戳数组,如下所示:

var offset = 5*60*60000

dtMin = new Date(+1420070400000 + offset);
dtMax = new Date(+1575158400000 + offset);

console.log("dtmin: ", dtMin);
console.log("dtmax: ", dtMax);

while (dtMin <= dtMax) {

  dtRng.push((dtMin.getTime() - offset).toString());

  dtMin = new Date(new Date(dtMin).setMonth(dtMin.getMonth()+1));

}

console.log("dt rng:", JSON.stringify(dtRng));

然后将其作为数组返回:

["1420070400000","1422748800000","1425168000000","1427842800000","1430434800000","1433113200000","1435705200000","1438383600000","1441062000000","1443654000000","1446332400000","1448928000000","1451606400000","1454284800000","1456790400000","1459465200000","1462057200000","1464735600000","1467327600000","1470006000000","1472684400000","1475276400000","1477954800000","1480550400000","1483228800000","1485907200000","1488326400000","1491001200000","1493593200000","1496271600000","1498863600000","1501542000000","1504220400000","1506812400000","1509490800000","1512086400000","1514764800000","1517443200000","1519862400000","1522537200000","1525129200000","1527807600000","1530399600000","1533078000000","1535756400000","1538348400000","1541026800000","1543622400000","1546300800000","1548979200000","1551398400000","1554073200000","1556665200000","1559343600000","1561935600000","1564614000000","1567292400000","1569884400000","1572562800000","1575158400000"]

但有时会在那里拉回31天或30天,如:

Epoch date  Human readable date (GMT) 
1420070400  2015-01-01 00:00:00
1422748800  2015-02-01 00:00:00
1425168000  2015-03-01 00:00:00
1427842800  2015-03-31 23:00:00
1430434800  2015-04-30 23:00:00
1433113200  2015-05-31 23:00:00
1435705200  2015-06-30 23:00:00
1438383600  2015-07-31 23:00:00

如果我按月递增,为什么会这样做?

此外,我的minDate和maxDate可能会有所不同......对于instnace,minDate可能是1464739200000(Wed,2016年6月1日00:00:00),最大日期可能是1488326400000(2017年3月1日星期三00:00: 00)...

另外,为什么它在前3个月准确地做到了,而不是之后的那些......行为似乎很奇怪......

--- ----- EDIT

尝试使用momentjs并将while部分更改为:

   while (dtMin <= dtMax) {

       dtRng.push(dtMin);

       dtMin = moment(dtMin).add(1, 'months').toDate();
       console.log("dtmin: ", dtMin);
   }

这里发生了一些奇怪的事情...控制台打印出来:

Sun Feb 01 2015 00:00:00 GMT-0500 (EST)
Sun Mar 01 2015 00:00:00 GMT-0500 (EST)
Wed Apr 01 2015 00:00:00 GMT-0400 (EDT)
Fri May 01 2015 00:00:00 GMT-0400 (EDT)
Mon Jun 01 2015 00:00:00 GMT-0400 (EDT)
Wed Jul 01 2015 00:00:00 GMT-0400 (EDT)
Sat Aug 01 2015 00:00:00 GMT-0400 (EDT)
Tue Sep 01 2015 00:00:00 GMT-0400 (EDT)

但推送到dtRng的时间戳看起来像这样,请注意第30天,第31天和第23天:

Epoch date  Human readable date (GMT) 
1422748800  2015-02-01 00:00:00
1425168000  2015-03-01 00:00:00
1427842800  2015-03-31 23:00:00
1430434800  2015-04-30 23:00:00
1433113200  2015-05-31 23:00:00
1435705200  2015-06-30 23:00:00
1438383600  2015-07-31 23:00:00
1441062000  2015-08-31 23:00:00
1443654000  2015-09-30 23:00:00

它应该返回:

Epoch date  Human readable date (GMT) 
1422748800  2015-02-01 00:00:00
1425168000  2015-03-01 00:00:00
1427846400  2015-04-01 00:00:00
1430438400  2015-05-01 00:00:00
1433116800  2015-06-01 00:00:00
1435708800  2015-07-01 00:00:00
1438387200  2015-08-01 00:00:00
1441065600  2015-09-01 00:00:00

1 个答案:

答案 0 :(得分:0)

从最新的更新看,you've run into a timezone issue似乎是从东部标准时间(EST)过渡到东部夏令时(EDT)一小时后丢失(或获得!?)的原因,这就解释了为什么要打印某些日期23:00:00

虽然dealing with timezones可以是a bit tricky,但针对此特定问题,我建议使用更简单的方法,使用Date.UTC函数创建UTC中的所有日期>。一个简单的循环就是遍历所有年份的所有月份:

for(var year = 2015; year < 2020; year++)
    for(var month = 0; month < 12; month++)
        dtRng.push(new Date(Date.UTC(year, month, 1, 00, 00, 00)));

输出console.log(JSON.stringify(dtRng))似乎给出了正确的结果:

"2015-02-01T00:00:00.000Z",
"2015-03-01T00:00:00.000Z",
"2015-04-01T00:00:00.000Z",
"2015-05-01T00:00:00.000Z",
"2015-06-01T00:00:00.000Z",
....

修改是以3个部分循环开始和结束日期:

  • 第一年,从月开始到12月。
  • 除了最后一年之外的所有年份,每年都要经过1至12个月。
  • 去年,循环播放1到上个月。

以下代码以(2015/03/01 to 2019/09/01)格式显示日期范围YYYY/MM/DD

// Initialize the date range.
var startYear = 2015, startMonth = 03,
      endYear = 2019,   endMonth = 09;

// Loop through the years.
for(var year = startYear; year <= endYear; year++) {
    var currStartMonth = 1, currEndMonth = 12;

    // If it's the first year, skip the months before the range.
    if (year === startYear) currStartMonth = startMonth;

    // If it's the last year, skip the months after the range.
    // Note that this also gracefully handles the case when,
    // startYear === endYear.
    if (year === endYear)     currEndMonth = endMonth;

    // Loop through the months and add it to the dates array.
    // '- 1' because of 0-indexing of month.
    for (var month = currStartMonth - 1; month < currEndMonth; month++) 
        dtRng.push(new Date(Date.UTC(year, month, 1, 00, 00, 00)));
}