I don't even know how to phrase the question properly but I am trying to calculate the total time a device is switched on. I have a mysql database which logs the time the device is either turned on or off.
an example of what my database looks like:
device id logtime status
---------------------------------------------------
17x1p14e6662 April 12th 2017, 1:05:52 pm ON
17x1p14e6662 April 12th 2017, 1:06:34 pm OFF
17x1p14e6662 April 12th 2017, 1:07:02 pm ON
17x1p14e6662 April 12th 2017, 1:14:00 pm OFF
17x1p14e6662 April 12th 2017, 1:34:52 pm ON
17x1p14e6662 April 12th 2017, 3:25:20 pm OFF
17x1p14e6662 April 12th 2017, 5:30:00 pm ON
I know how to calculate the difference between two time periods using momentJS, something like this (referenced from Get the time difference between two datetimes) :
var now = "04/09/2013 15:00:00";
var then = "02/09/2013 14:20:30";
var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
var d = moment.duration(ms);
var s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");
If I retrieve all records for a device, how can I only calculate the total times between each "ON" and "OFF" ?