Skip to main content

JavaScript Date Arithmetic — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Date Arithmetic lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Count the days between

    easy10 min

    Report how many days separate the two dates.

  2. 2

    Add days to a date

    easy10 min

    Add ten days and report the new day of the month.

  3. 3

    Compare two dates

    easy10 min

    Report whether the first date is earlier.

  4. 4

    Test for the same moment

    easy10 min

    Compare timestamps to check two dates match.

  5. 5

    Roll into the next month

    medium10 min

    Add five days to January 30 and report the month number.

Find the bug(5)

  1. 1

    Fix dates compared with triple equals

    easy8 min

    Two Date objects are never identical, so compare timestamps.

  2. 2

    Fix the wrong milliseconds per day

    easy8 min

    A day is 1000 times 60 times 60 times 24 milliseconds.

  3. 3

    Fix the days added to the wrong part

    easy8 min

    Adding to the month moves it far too far.

  4. 4

    Fix the subtraction reversed

    easy8 min

    The later date must come first to give a positive gap.

  5. 5

    Fix the shared date changed

    easy8 min

    The helper changed the caller's date instead of a copy.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace