- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我将最小日期拉为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
最佳答案
从最新的更新来看,好像是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",
....
修改是分三部分循环开始和结束日期:
以下代码以 YYYY/MM/DD
格式演示了日期范围(2015/03/01 至 2019/09/01)
的情况。
// 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)));
}
关于javascript - 创建时间戳记中的日期数组,增量为 1 年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41109568/
我希望在 vim 缓冲区中包含以下内容,最好独立于操作系统。 键入 today 将替换为 =20130716。 需要等号! 输入 tomorrow 将替换为 =20130717 键入 nextweek
我在尝试显示时间为DURATION的缩略图时遇到了困难。该代码呈现缩略图,但是没有任何时间。此外, View 的结果是O View 的结果。其他所有内容都可以正确获取。 entry as $e
我是一名优秀的程序员,十分优秀!