I'm tracking the usage of a device, and showing users their usage trends by hour. Basically, I need to look through times documents and see if the device was "on" betwen 6pm and 7pm, then 7pm and 8pm, the 8pm and 9pm, and so on for all 24 hours.
This is what I originally wrote:
// loop through 24 hours of the day
for (let j = 0; j < 24; j++) {
const beginningOfHour = moment().startOf('day').add(j, 'hour').toDate()
const endOfHour = moment(beginningOfHour).add({ m: 59, s: 59, ms: 999 }).toDate()
const cumulativeHours = timePerPeriod(beginningOfHour, endOfHour, myTimes) // calculation func
trendsArray.push(cumulativeHours)
}
This doesn't work becasue beginningOfHour and endOfHour also have a date attached to them.
I can't figure out how to check my times documents for just the hour portion, without any reference to date. Any advice on how to do this?
EDIT:
Here is what a times document looks like:
_id: 5e56cf9ae65bf2b30a6ab525 // ObjectID type
user_id: 5e56bb2b40ad526200401773 // ObjectID type
start: 2020-02-27T00:00:00.000+00:00 // Date type
stop: 2020-02-27T06:00:00.000+00:00 // Date type
The device can be on for any length of time, like 3 months straight or 15 seconds. Each time document is examined, and if it is on for any length of time during that hour duration, the length in miliseconds that it was on during that hour period is returned by my calculation function (called cumulativeHours above).