0

Hello I have this code using javascript :

var begin1 = new Date('December 17, 1995 08:00:00');    
var begin2 = new Date('December 17, 1995 08:01:00');
var begin3 = new Date('December 17, 1995 10:00:00');
var begin4 = new Date('December 17, 1995 16:00:00');

var end1 = new Date('December 17, 1995 08:30:00');
var end2 = new Date('December 17, 1995 09:00:00');
var end3 = new Date('December 17, 1995 11:00:00');
var end4 = new Date('December 17, 1995 16:30:00');

begin = [begin1, begin2, begin3, begin4];
end = [end1, end2, end3, end4];

Basically, I have many slots like begin1 - end1, begin2 - end2, begin3 - end3, begin4 - end4.

What I am trying to do is to evaluate the time like this :

for begin1 (08:00:00) - end1 (08:30:00) : 30 minutes

for begin2 (08:01:00) - end2 (09:00:00) : 59 minutes but if I look at the var begin1 there is the part (08:00:00) - (08:30:00) so for begin2 (08:01:00) - end2 (09:00:00) we count only the part (08:30:00) - (09:00) so it is 30 minutes

for begin3 (10:00:00) - end3 (11:00:00) : one hour

for begin4 (16:00:00) - end3 (16:30:00) : 30 minutes

so if I evaluate the sum I get 30 minutes + 30 minutes + 1 hour + 30 minutes = 2h30.

How can I do to get this result using javascript ?

Thank you a lot !

EDIT : Basically, I mean I have many dates for one day and I want to evaluate the time for this date for instance if I have 08:00:00-08:30:00 it is 30 minutes but if have also 08:10:00-09:00:00 I want to have just 08:30:00-09:00:00 because I have 08:00:00 -08:30:00 yet. So the duration will be one hour.

EDIT1 : This picture will explain exactly what I want :

result

Pierre
  • 47
  • 3
  • @Jamiec It's not a rude website - it's imho rude, to post some task, having made no attempt, on a topic, that has been asked thousands of times on SO/JS already. If they at least googled, mentioned a few existing topics, and why they don't answer their problem, that would be different. The links explain this, but apparently, they are "rude" and "should be removed". – ASDFGerte Nov 07 '19 at 10:07
  • So where is the duplicated question if this question has been asked thousands of times ? Sorry but this is the goal of this website to help people. – Pierre Nov 07 '19 at 10:11
  • That edit brings a *whole lot* more complexity. – Jamiec Nov 07 '19 at 10:20

2 Answers2

0

Your post is not clear.

If I understood it well you can do like this:

var date1 = new Date('December 17, 1995 03:24:00');
var date2 = new Date('December 17, 1995 03:34:00');
minutes = ((date2-date1)/1000)/60; //Get minutes of difference 

EDIT

I create a sample script but is not tested well. It should work if you have the data ordered according to begin:

function f(){
  var begin1 = new Date('December 17, 1995 08:00:00');  
  var begin2 = new Date('December 17, 1995 08:01:00');
  var begin3 = new Date('December 17, 1995 08:10:00');
  var begin4 = new Date('December 17, 1995 10:00:00');
  var begin5 = new Date('December 17, 1995 16:00:00');

  var end1 = new Date('December 17, 1995 08:30:00');
  var end2 = new Date('December 17, 1995 09:00:00');
  var end3 = new Date('December 17, 1995 09:20:00');
  var end4 = new Date('December 17, 1995 11:00:00');
  var end5 = new Date('December 17, 1995 16:30:00');

  begin = [begin1, begin2, begin3, begin4, begin5 ];
  end = [end1, end2, end3, end4, end5];

  duration = [[begin1, end1]];
  console.log(duration.length);
  for(i=1 ; i<begin.length; i++){
    for(j=0; j<duration.length; j++){
      if(begin[i]>= duration[j][0] && begin[i]<= duration[j][1] ){
         if(end[i]<= duration[j][1] ){
           break;
         }else{
           duration[j][1] = end[i];
           break;
         }
    }else{
      if(j==duration.length-1){
        console.log(begin[i]);
        duration.push([begin[i], end[i]]);
        break;
      }else{
        continue;
      }

    }
  }
  }
  var diff = 0;
  var i = 1;
  duration.forEach(function(element){
      console.log("Time range " + i + ": " + element);
      diff= diff + (element[1]- element[0])/1000/60;
      i++;
  });
    console.log("Total Minutes: " + diff);
  }

f();
Wolfetto
  • 1,030
  • 7
  • 16
  • No I mean I have many dates for one day and I want to evaluate the time for this date for instance if I have 08:00:00-08:30:00 it is 30 minutes but if have also 08:10:00-09:00:00 I want to have just 08:30:00-09:00:00 because I have 08:00:00 -08:30:00 yet. So the duration will be one hour. – Pierre Nov 07 '19 at 10:10
  • now is more clear.. Please edit your question to make it more clear – Wolfetto Nov 07 '19 at 10:12
  • @Pierre try the above snippet – Wolfetto Nov 07 '19 at 13:10
0

To start out, understand that parsing dates in that manner is potentially problematic. Some good reads on the subject are here:


But, assuming you understand the limitations and that the objects returned from your code provide acceptable results for you the answer is fairly straightforward.

The first thing to know is that you can directly subtract one date from another to get the difference (in ms). Some simple arithemtic will convert this to seconds, or minutes.

var begin1 = new Date('December 17, 1995 08:00:00');    

var end1 = new Date('December 17, 1995 08:30:00');

var diffMs = end1 - begin1;
console.log(diffMs + "ms")

var diffS = diffMs/1000;
console.log(diffS + "s");

var diffM = diffS/60;
console.log(diffM + "mins");

Now, to do this across your arrays, you could use a function that zips them together and applies a function which calculates the difference

var begin1 = new Date('December 17, 1995 08:00:00');    
var begin2 = new Date('December 17, 1995 08:01:00');
var begin3 = new Date('December 17, 1995 10:00:00');
var begin4 = new Date('December 17, 1995 16:00:00');

var end1 = new Date('December 17, 1995 08:30:00');
var end2 = new Date('December 17, 1995 09:00:00');
var end3 = new Date('December 17, 1995 11:00:00');
var end4 = new Date('December 17, 1995 16:30:00');

var begin = [begin1, begin2, begin3, begin4];
var end = [end1, end2, end3, end4];

var zip = (array1,array2, func) => array1.map( (v,i) => func(v,array2[i]));

var diffs = zip(begin,end, (b,e) => e - b);
console.log(diffs);

var result = diffs.reduce( (acc,e) => acc + e,0);
console.log("Total MS", result);
console.log("Total S", result/1000);
console.log("Total M", result/1000/60);

Above you can see the result is a new array with 4 elements - each being the difference in milliseconds. You can then reduce this to get the sum in Ms (result).

You may then use the arithmetic specified at the beginning of this answer to display this how you wish.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Thank you but the result should be 180 minutes not 179 minutes = 10740000 ms ? – Pierre Nov 07 '19 at 10:19
  • @Pierre 30+59+60+30=179. Are you talking about your extra complexity of trying to sort out overlapping times? In which case I think you're after 150 minutes. – Jamiec Nov 07 '19 at 10:22
  • Sorry I mean it should be 30 + (60-30) + 60 + 30 = 150 minutes because for 08:00:00 - 08:30:00 it is 30 minutes and then for 08:01:00 - 09:00:00 it is 59 minutes but we have 08:00:00 - 08:30:00 yet so it is 30 minutes and then we get 60 minutes (08:00:00 - 09:00:00) I mean – Pierre Nov 07 '19 at 10:27
  • @Pierre can you ensure the dates are ordered in `begin` and `end`? – Jamiec Nov 07 '19 at 10:29
  • Basically the end of begin1 is end1, the end of begin2 is end2, the end of begin3 is end3, the end of begin4 is end4 – Pierre Nov 07 '19 at 10:31
  • @Pierre yes, I got that. But are they in date order? – Jamiec Nov 07 '19 at 10:32