Skip to curriculum
Jump to the editor

1.8. Days between two dates, and 12-hour to 24-hour time

Medium
Topics
15–25 min

"Due in 3 days", "booked for 2 nights", "posted yesterday": interfaces count calendar days all the time, and the obvious code, subtracting two dates and dividing by the milliseconds in a day, gets it wrong whenever the times of day differ or a daylight saving change falls in between. Times typed by people have the same problem in a different form: "12:30 AM" is just after midnight, and "12:30 PM" is just after noon.

Write two functions in dates.js: daysBetween(start, end) and to24Hour(time).

Signature

JAVASCRIPT
function daysBetween(start, end) // a whole number of days
function to24Hour(time)          // "HH:MM"
  • start and end are Date objects. The count uses their dates in local time; the time of day does not matter.
  • time is a string such as "1:05 PM".

Requirements

  1. daysBetween returns the number of calendar days from start's date to end's date: 0 for the same date, 1 for the next date, whatever the times of day (23:00 to 01:00 the next day is 1; 01:00 to 23:00 the next day is also 1).
  2. It is negative when end is earlier than start.
  3. It is exact across month and year ends, leap years (including 1900, not a leap year, and 2000, a leap year) and long ranges, and always a whole number. It works for any year a Date can hold, including years 0 to 99 and years before 0.
  4. It does not change the Date objects passed in.
  5. It throws a TypeError when an argument is not a Date, and a RangeError when an argument is an Invalid Date.
  6. to24Hour converts a 12-hour time to "HH:MM" with two-digit hours: "1:05 PM" is "13:05", "9:15 AM" is "09:15".
  7. "12:xx AM" becomes "00:xx" (midnight) and "12:xx PM" stays "12:xx" (noon).
  8. It accepts one or two hour digits, exactly two minute digits, AM or PM in any case with at most one space before it, and spaces around the whole time.
  9. It throws a RangeError for hours outside 1 to 12, minutes outside 00 to 59, or any string not in that form, and a TypeError for a value that is not a string.

Examples

JAVASCRIPT
daysBetween(new Date(2024, 4, 10, 23, 0), new Date(2024, 4, 11, 1, 0)); // 1
daysBetween(new Date(2024, 1, 28), new Date(2024, 2, 1));               // 2: 2024 has 29 February
daysBetween(new Date(2024, 4, 11), new Date(2024, 4, 4));               // -7
daysBetween("2024-05-10", new Date());                                  // throws TypeError
JAVASCRIPT
to24Hour("1:05 PM");   // "13:05"
to24Hour("12:00 AM");  // "00:00"
to24Hour("12:30 PM");  // "12:30"
to24Hour("7:45pm");    // "19:45"
to24Hour("13:00 PM");  // throws RangeError

Environment

  • Plain JavaScript in a browser page; the tests call the globals daysBetween and to24Hour from dates.js.
  • The tests make dates with new Date(year, monthIndex, day, hours, minutes), in this computer's time zone, and with setFullYear for years 0 to 99 and before 0 (the constructor would read 99 as 1999). Months count from 0.
  • Any built-in may be used. Date libraries are not available.

Hints

  1. Two instants on consecutive dates can be 2 hours apart or 46 hours apart. What could you compare instead of the instants?
  2. Where would a day be 23 or 25 hours long, and which time standard never has such days?
  3. What does 12 % 12 give?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…