1

What is the best way to calculate time intervals (with or without some lib)?

Case:

I have three parameters:

startTime is an instance of Date;

endTime an instance of Date;

interval is of type "number";

Passing these parameters to some function, I expect an array of Date instances with the date of the intervals.

Example:

const startDateTime = new Date(2022, 1, 1);
startDateTime.setHours(9, 0, 0, 0);
const endDateTime = new Date(2022, 1, 1);
endDateTime.setHours(10, 30, 0, 0);

calculateDateTimeIntervals(startTime, endTime, 1800) //the last param is the time interval in seconds: 1800 is 30min

//expect array Date in time format to simplify:  ['09:00', '09:30', '10:00', '10:30']; 

  • 1
    check [this](https://stackoverflow.com/a/68835899/5058682) answer, it's more readable. – Amit Kumar Sep 27 '22 at 11:56
  • You could do `const startDateTime = new Date(2022, 1, 1, 9)`. It's not very semantic to declare a constant then modify it immediately afterward. :-( – RobG Sep 28 '22 at 04:34

1 Answers1

1

I think you can simplify your code with a while loop instead of calculating intervalls.

const getDates = (start, end, interval) => {
  const dates = [];
  let date = start;

  while(date <= end) {
    dates.push(date);
    // adjusted according to @RobG
    date = new Date(date.setSeconds(date.getSeconds() + interval));
  }
  return dates;
}


const start = new Date(2022, 1, 1);
start.setHours(9, 0, 0, 0);
const end = new Date(2022, 1, 1);
end.setHours(10, 30, 0, 0);

const interval = 1800;

console.log(getDates(start, end, interval));
console.log(start);
kevinSpaceyIsKeyserSöze
  • 3,693
  • 2
  • 16
  • 25
  • thanks! I just would include this: let date = start; to: const date = {...start}; to avoid some reference to startDate – Fabio Ribeiro de Carvalho Sep 27 '22 at 14:18
  • @FábioRibeirodeCarvalho I don't really know what you mean. As it is right now, will work because of scopes. But feel free to change it as you want:) – kevinSpaceyIsKeyserSöze Sep 27 '22 at 14:22
  • when you change the let date you will change the start as well because the date is reference to start. So, if nedded change start for some reasen, we lost the value...not? – Fabio Ribeiro de Carvalho Sep 27 '22 at 14:26
  • Note that by directly modifying the time value, the local time will change if the range crosses a daylight saving (DST) boundary by the DST offset. In places that change at midnight, that might also affect the local date. But it does mean it will be a constant amount of elapsed time. – RobG Sep 28 '22 at 04:40
  • 1
    @FábioRibeirodeCarvalho it does not seem to change the start date as the `console.log` shows. If you want to be sure you can change `let date = start;` to `let date = new Date(start);` – kevinSpaceyIsKeyserSöze Sep 28 '22 at 06:33
  • 2
    @FábioRibeirodeCarvalho—it doesn't change the start date. On the first iteration it creates a new date with a time value that is effectively `start.getTime() + interval`, it does not modify *start*. After that, it uses the date from the previous iteration. However, since there is `dates.push(date)`, the first date in the array is a reference to *start*, so if a subsequent process modifies *that* date, it will change the date passed as *start*. So it would be best to start with `let date = new Date(start)`. – RobG Sep 28 '22 at 06:55
  • @RobG what would be the solution to the crossing daylight range? – kevinSpaceyIsKeyserSöze Sep 28 '22 at 06:56
  • 1
    Use local methods, so if the interval is say minutes, then increment by `date.setMintutes(date.getMinutes() + interval)`. If the interval is hours, use *set/getHours*, and so on. Or convert to a base unit, say milliseconds, and use *get/setMilliseconds*. – RobG Sep 28 '22 at 06:58