0

I have an array of tracks.

const history = [
  {
    entryDate: "2022-05-03T09:32:07.137+02:00",
    type: "statusChange",
    message: "",

  },
  {
    entryDate: "2022-05-02T19:32:07.137+02:00",
    type: "statusChange",
    message: "",
  },

  {
    entryDate: "2022-05-02T09:32:07.137+02:00",
    type: "statusChange",
    message: "",
  },
  {
    entryDate: "2022-05-02T07:30:01.672+02:00",
    type: "statusChange",
    message: "",
  }
];

How to get the number of minutes that was tracked only during given times, for example: How many minutes was the user tracked from 09:30 - 18:00 from the given array of tracks?

Hard to figure it out myself.

Sebastian Lagua
  • 380
  • 1
  • 6
  • 17
  • Check https://stackoverflow.com/questions/7709803/javascript-get-minutes-between-two-dates – James Nov 30 '22 at 20:20
  • Hey James, thank you for your reply. But it's not about the difference between two dates (which is pretty simple), Im talking about calculating minutes between 2 dates and the time of a given time. For Example: I have track started at 07:30 and it ends at 10:00 and I know that working hours are from 09:00 to 18:00 I need to calculate the minutes between them. (which will be 60 minutes) – Sebastian Lagua Nov 30 '22 at 20:28

1 Answers1

1

Not sure how you know which elements in the array are “workstart” vs “workend” events. Assuming you have that part figured out, and that you have four date variables workStart, workEnd, trackStart, trackEnd, which are numeric:

let start = Math.max(workstart, trackstart);
let end = Math.min(workend, trackend);
let result = end > start ? end - start : 0;
James
  • 20,957
  • 5
  • 26
  • 41
  • the last element in the array it's a start track. First, it's the end track. but it's a date, does Math.max can compare a date? – Sebastian Lagua Nov 30 '22 at 21:25
  • it works partially; for example, it calculates everything if the track happens during one exact date. It would be interesting if it's possible to split it for multiple days. Or better to iterate between pairs of dates and found amount of minutes? What do you think will be a better approach? – Sebastian Lagua Nov 30 '22 at 21:36
  • It they are JavaScript Date objects then yes. result will be in milliseconds. – James Nov 30 '22 at 21:38
  • I createad samll sandbox to show result: https://codesandbox.io/s/xenodochial-dan-9530mn?file=/src/index.js - Looks like a value not correct :( total minutes in this example must be 602 minutes, but I got 1472 – Sebastian Lagua Dec 01 '22 at 09:17
  • Start and End dates should all be on the same day. – James Dec 01 '22 at 13:31