-1

I have the following data set. I have an array of objects like this

[{ 
    createdAt: Wed Feb 08 2017 12:16:35 GMT+0500 (Pakistan Standard Time),
    set: 'jr',
},{ 
    createdAt: Wed Feb 08 2017 12:18:36 GMT+0500 (Pakistan Standard Time),
    or: 'd',
},{ 
    createdAt: Wed Feb 08 2017 12:22:46 GMT+0500 (Pakistan Standard Time),
    or: 'd',
},]

I want to calculate the time difference between each createdAt, for example from 1st two createdAt fields it will give '2 minutes'.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
DEO
  • 316
  • 2
  • 4
  • 18

3 Answers3

2

Firstly note that for this to work the date values need to be strings. Then you can use a simple for loop to iterate through the array, converting the strings to Date objects and comparing the current date value to that in the next array element. From there you can calculate the number of minutes between the dates. Try this:

var data = [{
  createdAt: 'Wed Feb 08 2017 12:16:35 GMT+0500 (Pakistan Standard Time)',
  set: 'jr'
}, {
  createdAt: 'Wed Feb 08 2017 12:18:36 GMT+0500 (Pakistan Standard Time)',
  or: 'd'
}, {
  createdAt: 'Wed Feb 08 2017 12:22:46 GMT+0500 (Pakistan Standard Time)',
  or: 'd'
}]

var differences = [];
for (var i = 0; i < data.length - 1; i++) {
  var diff = new Date(data[i + 1].createdAt) - new Date(data[i].createdAt);
  var diffMins = new Date(diff).getMinutes();
  differences.push(diffMins);
}

console.log(differences); 

Note that the above logic depends on the dates being ordered ascending.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • well it works but can you tell me that how will i get all values outside loop function . i think new array will be required to push values in it can you guide me – DEO Feb 08 '17 at 11:05
  • You could push it to an array - I added an example for you – Rory McCrossan Feb 08 '17 at 11:14
  • thanks i have done the 2nd part . the main problem i am facing is that i want to add both of these arrays like this ` [{ createdAt: 'Wed Feb 08 2017 12:16:35 GMT+0500 (Pakistan Standard Time)', set: 'jr' }, { createdAt: 'Wed Feb 08 2017 12:18:36 GMT+0500 (Pakistan Standard Time)', or: 'd', differnce,2 }, { createdAt: 'Wed Feb 08 2017 12:22:46 GMT+0500 (Pakistan Standard Time)', or: 'd', differnce: 4 }] ` – DEO Feb 08 '17 at 11:31
0
var a = new Date(arr[0].createdAt);
var b = new Date(arr[1].createdAt);
var diff;

if(a > b) {
    diff = a.getTime() - b.getTime();
} else {
    diff = b.getTime() - a.getTime();
}

diff = diff/1000;

// now diff is in seconds
// calculate minutes by dividing it by 60
// use this in for loop
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Ronn Wilder
  • 1,228
  • 9
  • 13
0

convert date to unix timestamp in seconds. then you can calculate the difference in seconds by arithmetic operation like below pseudo code.

var d1 = Date.parse('Wed Feb 08 2017 12:16:35 GMT+0500')/1000 //1486538195
var d2 = Date.parse('Wed Feb 08 2017 12:18:35 GMT+0500')/1000 //1486538315
var diff = Math.floor((d2 - d1)/60) + 'Min' + (d2-d1)%60 + 'Sec'

Similarly you can iterate over get each difference.

Jagajit Prusty
  • 2,070
  • 2
  • 21
  • 39