The time card calculator on mytimecardcal.com has a small toggle at the top: 12-hour or 24-hour display. It looks like the easiest feature on the whole page. A checkbox, a bit of formatting, done by lunch. It took me three rounds to get right, and every round taught me something about how people actually read a clock.
The first version did the obvious thing. Times were stored as minutes since midnight, an integer, and the display function converted that integer into a string. Twelve-hour mode divided by 60, took the remainder, added AM or PM. Twenty-four-hour mode padded the hours to two digits. Simple, and wrong in four places at once.
The first bug was midnight. In 12-hour mode, 0 minutes past midnight rendered as 0:00 AM. Nobody says 0:00 AM. Midnight is 12:00 AM, and noon is 12:00 PM. The fix is a line every programmer writes once and then never forgets:
let h12 = hours % 12; if (h12 === 0) h12 = 12; That one line handles both midnight and noon, because 12 % 12 is 0 too. I had written it in the first draft of the parser and then lost it when I rewrote the display code, which is exactly the kind of bug that happens when the same conversion lives in two places.
The second bug was the inputs. People type their clock-in time into a text field, and the parser accepted 9, 9:30, 9:30a, 5p and 17:30. When the toggle was on 24-hour, a user typing 9:30 meant 09:30 in the morning, which was fine. But a user who typed 5p in 24-hour mode expected 17:00, and my parser returned 5 in the morning because it stripped the letter before it checked it. The display mode should never change what an input means. The parser now works purely on the raw text — the letters a and p decide AM and PM, and a bare number is decided by the clock-in/clock-out rule — and the toggle only ever touches the output. Input and display are two separate jobs, and the moment they share a code path, something breaks.
The third bug was the totals row. The daily totals were formatted with the same display function as the clock times, so in 12-hour mode a day of 8 hours and 30 minutes showed as 8:30 AM. A duration is not a time of day, and formatting one like the other is a category error. Durations now go through their own formatter that never adds AM or PM and never wraps at 12. It sounds obvious written down, but the totals row reused the time formatter because it was there, and that is how the bug shipped.
The fourth bug was the saved state. The toggle choice is saved so the next visit remembers it, and the saved value was a string, "12" or "24". After a reload, a strict comparison against the number 12 failed silently, the toggle showed 12-hour while the times rendered in 24-hour, and the two disagreed until the user clicked the toggle twice. Saving a preference is easy; reading it back in the same type is the part people skip. I now store the value as a boolean, use24Hour, and the toggle, the display code and the saved value all read that one variable.
The lesson I keep relearning on this site is that display code is never as small as it looks. The calculation underneath — minutes in, minutes out, overtime rules on top — is the honest part. The display is where a real person meets the number, and that is where midnight becomes 0:00 AM, a duration becomes a time of day, and a saved setting quietly stops working. The toggle is now about forty lines of code, including the tests I wrote for 00:00, 12:00, 12:30 and 23:59 in both modes. Forty lines for one checkbox is not over-engineering. It is what the checkbox actually costs.