diff --git a/src/impl/tokenParser.js b/src/impl/tokenParser.js index 48a7595e..202be01e 100644 --- a/src/impl/tokenParser.js +++ b/src/impl/tokenParser.js @@ -451,10 +451,13 @@ export class TokenParser { if (!this.isValid) { return { input, tokens: this.tokens, invalidReason: this.invalidReason }; } else { - const [rawMatches, matches] = match(input, this.regex, this.handlers), - [result, zone, specificOffset] = matches - ? dateTimeFromMatches(matches) - : [null, null, undefined]; + const [rawMatches, matches] = match(input, this.regex, this.handlers); + if (hasOwnProperty(matches, "h") && matches.h > 12) { + throw new ConflictingSpecificationError("Can't go over 12 when specifying 12-hour format"); + } + const [result, zone, specificOffset] = matches + ? dateTimeFromMatches(matches) + : [null, null, undefined]; if (hasOwnProperty(matches, "a") && hasOwnProperty(matches, "H")) { throw new ConflictingSpecificationError( "Can't include meridiem when specifying 24-hour format" diff --git a/test/datetime/tokenParse.test.js b/test/datetime/tokenParse.test.js index 8b5c6a8d..d8839846 100644 --- a/test/datetime/tokenParse.test.js +++ b/test/datetime/tokenParse.test.js @@ -74,6 +74,12 @@ test("DateTime.fromFormat() throws if you specify meridiem with 24-hour time", ( expect(() => DateTime.fromFormat("930PM", "Hmma")).toThrow(ConflictingSpecificationError); }); +test("DateTime.fromFormat() throws if h is used with 24-hour time", () => { + expect(() => DateTime.fromFormat("18:30 AM", "h:mm a")).toThrowError( + ConflictingSpecificationError + ); +}); + // #714 test("DateTime.fromFormat() makes dots optional and handles non breakable spaces", () => { function parseMeridiem(input, isAM) {