Skip to content

Commit

Permalink
added check for "h"
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Carr committed Jan 6, 2024
1 parent 14af2d2 commit a6f3b55
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/impl/tokenParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ export function explainFromTokens(locale, input, format) {
} else if (
hasOwnProperty(matches, "h") &&
matches["h"] > 12 &&
tokens.find((t) => t.val === "hh")
tokens.find((t) => t.val === "h" || t.val === "hh")
) {
const hourValue = matches["h"];
return {
Expand Down
11 changes: 10 additions & 1 deletion test/datetime/tokenParse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ test("DateTime.fromFormat() parses basic times", () => {
expect(i.millisecond).toBe(445);
});

test("DateTime.fromFormat() yields Invalid reason for invalid input", () => {
test("DateTime.fromFormat() yields Invalid reason for invalid 12-hour time", () => {
const i = DateTime.fromFormat("11-08-2023 15:00 AM", "MM-dd-yyyy h:mm a");
expect(i.invalid).not.toBeNull();
expect(i.invalid.reason).toEqual("unit out of range");
expect(i.invalid.explanation).toEqual(
"you specified 15 (of type number) as an hour along with a meridiem, which is invalid"
);
});

test("DateTime.fromFormat() yields Invalid reason for invalid 12-hour time with padding", () => {
const i = DateTime.fromFormat("11-08-2023 15:00 AM", "MM-dd-yyyy hh:mm a");
expect(i.invalid).not.toBeNull();
expect(i.invalid.reason).toEqual("unit out of range");
Expand Down

0 comments on commit a6f3b55

Please sign in to comment.