"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).
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).
It is negative when end is earlier than start.
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.
It does not change the Date objects passed in.
It throws a TypeError when an argument is not a Date, and a RangeError when an argument is an Invalid Date.
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".
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.
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.
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.