Created
January 29, 2020 12:44
-
-
Save missinglink/94e62c015198913cea80b935082493ed to your computer and use it in GitHub Desktop.
functions for computing time windows for data series
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const interval = { | |
millisecond: 1, | |
second: 1000, | |
minute: 1000 * 60, | |
hour: 1000 * 60 * 60, | |
day: 1000 * 60 * 60 * 24 | |
} | |
const period = 'day' | |
const d1 = truncate(new Date('2020-01-01 22:22:22'), period) | |
const d2 = truncate(new Date(), period) | |
console.error(d1) | |
console.error(d2) | |
for (let i = d1.getTime(); i <= d2.getTime(); i += interval[period]){ | |
console.error(i) | |
} | |
function truncate(d, period){ | |
const r = new Date(0) | |
r.setFullYear(d.getFullYear()) | |
if(period === 'year'){ return r } | |
r.setMonth(d.getMonth()) | |
if (period === 'month') { return r } | |
r.setDate(d.getDate()) | |
if (period === 'day') { return r } | |
r.setHours(d.getHours()) | |
if (period === 'hour') { return r } | |
r.setMinutes(d.getMinutes()) | |
if (period === 'minute') { return r } | |
r.setSeconds(d.getSeconds()) | |
if (period === 'second') { return r } | |
r.setMilliseconds(d.getMilliseconds()) | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment