0

I need a function to check how many hours have passed between two dates, but if for example there is a weekend in between, then skip those hours.

Date 1: moment('2022-05-27 20:00:00).tz('UTC')

Date 2: moment('2022-05-30 20:00:00).tz('UTC')

Expected output: 24 hours

  • This answer or that library might be useful: - https://stackoverflow.com/a/37069478/16806649 - https://www.npmjs.com/package/moment-business-days – codemode May 27 '22 at 21:16

1 Answers1

1

I get 25 if I do i <= n and 24 when I do i < n

But I do not use moment

Note that this should work across daylight savings changes in the locale you are in

const d1 = new Date('2022-05-27 20:00:00')
const d2 = new Date('2022-05-30 20:00:00')
console.log(d1, d2)
let hours = 0;
for (let i = d1.getTime(), n = d2.getTime(); i < n; i += 3600000) {
  const d = new Date(i);
  if (![0, 6].includes(d.getDay())) hours++; 
}
console.log(hours)
mplungjan
  • 169,008
  • 28
  • 173
  • 236