Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
How can I get the difference between two dates in hours, omitting the hours on weekends?

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!