Skip to content

fix(datetime): parse 12 AM as midnight and 12 PM as noon - #7376

Closed
Harsh23Kashyap wants to merge 1 commit into
denoland:mainfrom
Harsh23Kashyap:fix/datetime-parse-12-hour-clock
Closed

Harsh23Kashyap wants to merge 1 commit into
denoland:mainfrom
Harsh23Kashyap:fix/datetime-parse-12-hour-clock

Conversation

@Harsh23Kashyap

Copy link
Copy Markdown

Fixes #7370

parse() turned a 12-hour time into 24-hour by adding 12 for PM and doing nothing for AM. That is wrong for the hour 12: 12 AM is hour 0 and 12 PM is hour 12. So 12:xx AM parsed as noon and 12:xx PM parsed as hour 24, which rolled over to the next day. format() already prints midnight as 12 AM, so a formatted value did not round-trip.

The fix takes the hour modulo 12 for AM, and modulo 12 plus 12 for PM. It covers the AM, AM., A.M. and matching PM spellings, since they share the same code.

Reproduction (TZ=UTC, the script from the issue)

Before:

2024-01-01T12:00:00.000Z
2024-01-02T00:30:00.000Z
2024-01-01 12:30 AM false

After:

2024-01-01T00:00:00.000Z
2024-01-01T12:30:00.000Z
2024-01-01 12:30 AM true

Tests

Added to datetime/parse_test.ts: 12 AM and 12 PM in all three spellings, 1 to 11 o'clock still parsing as before, and a round trip of every hour of the day through format() and parse() with hh:mm a.

deno test datetime/ passes (22 tests, 50 steps) with TZ=UTC and TZ=America/New_York. With only the formatter change reverted, the 12 AM/PM test and the round-trip test fail, and the 1 to 11 o'clock test passes. deno fmt --check and deno lint are clean on the folder.

@Harsh23Kashyap

Copy link
Copy Markdown
Author

I missed that #7371 already fixes #7370. Closing this one in favor of #7371.

@codecov

codecov Bot commented Nov 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.02%. Comparing base (f834d02) to head (3f7ffd0).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7376      +/-   ##
==========================================
- Coverage   95.04%   95.02%   -0.02%     
==========================================
  Files         619      620       +1     
  Lines       52012    52016       +4     
  Branches     9450     9452       +2     
==========================================
- Hits        49434    49430       -4     
  Misses       2031     2031              
- Partials      547      555       +8     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

datetime: parse() reads 12 AM as noon and 12 PM as midnight of the next day

1 participant