Scriptdrill

Level 3: Strings and dates Exercise 24 of 45 Pro

Add days to a date

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

`isoDate` is a calendar date like `"2024-01-30"`. Return the date `n` days later (earlier when `n` is negative) in the same `YYYY-MM-DD` form. Work in UTC so the answer does not depend on the time zone of the computer, and get leap years right.

What this exercise teaches

The Z suffix makes the date midnight UTC, and the UTC setters never meet daylight saving changes. setUTCDate rolls over months and years, leap days included, and toISOString prints the UTC date.

A hint if you get stuck

Parse with `new Date(isoDate + "T00:00:00Z")`, move with `setUTCDate(getUTCDate() + n)`, and format with `toISOString().slice(0, 10)`.

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.

Start with the free exercises

More exercises in Strings and dates