Skip to content

Instantly share code, notes, and snippets.

@missinglink
Created January 29, 2020 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save missinglink/94e62c015198913cea80b935082493ed to your computer and use it in GitHub Desktop.
Save missinglink/94e62c015198913cea80b935082493ed to your computer and use it in GitHub Desktop.
functions for computing time windows for data series
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