Days 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
`a` and `b` are calendar dates like `"2024-01-31"`. Return the number of days from `a` to `b`: positive when `b` is later, negative when it is earlier, 0 for the same date. The answer must be right in every time zone, including across daylight saving changes.
What this exercise teaches
UTC days are always exactly 86,400,000 ms long, so the difference divides evenly. Local dates are not: a day with a daylight saving change has 23 or 25 hours, which breaks Math.floor on local times.
A hint if you get stuck
Turn both dates into UTC milliseconds with `Date.parse(date + "T00:00:00Z")`, subtract, and divide by `86400000`.
This exercise is part of Pro
Pro unlocks all 45 exercises, with worked solutions and progress saved to your account. The free exercises work without an account.
You create a free account first, so Pro is attached to it.
Monthly is charged today and then every month until you cancel, in one click from your account page. Lifetime is one payment with no renewals. Full refund within 14 days of paying.