Consider pulling the minimum date as 1420070400000
, which corresponds to 2015-01-01 00:00:00
, and the maximum date as 1575158400000
, which translates to 2019-12-01 00:00:00
.
Next, establish an array of timestamps for each month between those two dates using this script:
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));
The resulting array looks like this:
["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"]
However, sometimes it includes a 31-day or 30-day interval such as:
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
If you're incrementing by month, why is this happening?
Moreover, keep in mind that minDate and maxDate can fluctuate. For example, minDate could be 1464739200000 (Wed, 01 Jun 2016 00:00:00) while maxDate might be 1488326400000 (Wed, 01 Mar 2017 00:00:00)...
Also, why does it work correctly for the initial 3 months but not thereafter? The behavior seems erratic...
---EDIT-----
Considering using moment.js for this purpose, you can update the while
section with the following code:
while (dtMin <= dtMax) {
dtRng.push(dtMin);
dtMin = moment(dtMin).add(1, 'months').toDate();
console.log("dtmin: ", dtMin);
}
An unexpected outcome occurs here...the console displays:
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)
Nevertheless, the timestamps pushed to dtRng
look different, showing 30th and 31st days with 23 hours:
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
The expected output should appear like this:
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