Scriptdrill

Level 3: Strings and dates Exercise 42 of 76 Free

Weekdays between two dates

A JavaScript exercise from Scriptdrill. Solve it with a code editor in your browser that runs your function against example and hidden tests, and it is checked the moment you submit.

The task

`from` and `to` are dates written as `YYYY-MM-DD`, with `from` not after `to`. Return how many of the days from `from` up to and including `to` are weekdays (Monday to Friday). Treat the dates as UTC so daylight saving changes cannot add or drop a day. `businessDays("2024-05-15", "2024-05-15")` is 1 because that Wednesday counts.

What this exercise teaches

In UTC every day is exactly 86400000 ms, so stepping by that amount and asking getUTCDay never drifts. Using local time methods can skip or repeat a day around a daylight saving change, and a `<` loop condition silently leaves out the last day.

A hint if you get stuck

Build both ends with `Date.UTC(y, m - 1, d)`, then step 86400000 ms at a time while the time is `<=` the end, counting the days whose `getUTCDay()` is between 1 and 5.

Try it now, free, no account needed

Every exercise on Scriptdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. The worked solution opens once you solve it or after two tries.

Open this exercise

More exercises in Strings and dates