I have a collection like this:
[
{X:1, Date:"12/01/2017 12:30am"},
{X:3, Date:"12/01/2017 1am"},
{X:2, Date:"12/01/2017 3am"}
]
What Javascript/Typescript can I use to calculate the elapsed time based on the items in the set?
I have a collection like this:
[
{X:1, Date:"12/01/2017 12:30am"},
{X:3, Date:"12/01/2017 1am"},
{X:2, Date:"12/01/2017 3am"}
]
What Javascript/Typescript can I use to calculate the elapsed time based on the items in the set?
For your input like this
arr =[
{X:1, date:"2017-12-20 08:48:54"},
{X:3, date:"2017-12-20 09:48:54"},
{X:2, date:"2017-12-20 12:48:54"}
]
you can use
var elapsedTime = [];
for (var i =0; i< arr.length-1; i++) {
elapsedTime.push((new Date(arr[i+1].date) - new Date(arr[i].date))/(1000*60));
}
Here you are converting the value to date format and then subtracting which give you the difference in milliseconds.
To calculate the difference use this -
var diff = Math.max(...elapsedTime) - Math.min(...elapsedTime)