【问题标题】:creating an array of dates in timestamp incremented by 1 year在时间戳中创建一个日期数组,增量为 1 年
【发布时间】:2016-12-12 21:06:17
【问题描述】:

假设我将最小日期设为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 可能会有所不同...例如,minDate 可能是 1464739200000(2016 年 6 月 1 日星期三 00:00:00),最大日期可能是 1488326400000(2017 年 3 月 1 日星期三 00:00): 00)...

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

---编辑-----

为此尝试使用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

【问题讨论】:

  • 时区,总是时区。您应该考虑使用像 Moment.js 这样的库来帮助处理日期。另请参阅this question
  • @MikeMcCaughan,刚刚用 momentjs 尝试过,但似乎仍然无法正确...仍然出现奇怪的行为...
  • 您是如何获得这些“人类可读日期”的?确保您使用的是toISOString()JSON.stringify();否则它可能会显示当地时间而不是 UTC。

标签: javascript arrays datetime


【解决方案1】:

从最新的更新来看,you've run into a timezone issue 似乎在从东部标准时间 (EST) 转换到东部夏令时间 (EDT) 时损失(或增加!?)一个小时,这解释了为什么要打印 @987654325 @ 一些日期。

虽然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)));
}

【讨论】:

  • 如果我的最小和最大时间戳可能不同(有时它们可​​能在同一年)但它们总是会出现在当月的第一天......我怎么能在不修复的情况下做到这一点年?
  • 这可以轻松修改以处理同一年和/或从不同月份开始。您可以使用 Date.UTC 中的最后 3 个参数来添加小时、分钟和秒。
  • 你如何循环遍历它?例如,如果我的最小日期是 1464739200000 (Wed, 01 Jun 2016 00:00:00) 而我的最大日期是 1488326400000 (Wed, 01 Mar 2017 00:00:00),我将如何创建这两者之间的月份?
  • 这也是另一个问题,我原来的方式,如何才能准确地为前 3 个月增加一个月,然后为下一个不增加?
猜你喜欢
  • 1970-01-01
  • 2010-11-01
  • 2019-07-17
  • 1970-01-01
  • 2020-02-15
  • 1970-01-01
  • 1970-01-01
  • 2015-02-26
  • 1970-01-01
相关资源
最近更新 更多