Query guide: JavaScript time format regex
JavaScript regex time format validator for regex.test checks
Use this page when a JavaScript expression needs to accept or reject time strings before a form, scheduler, import row, or settings field moves forward. Test 24-hour times, optional seconds, AM/PM variants, and failing samples online before copying the expression into regex.test().
Searches for javascript regex time format validator, 24 hour time regex javascript, and check regex javascript usually point to a shape decision, not final scheduling logic. A regex can answer whether a string looks like an accepted time. It should not decide timezone conversion, calendar availability, daylight saving behavior, or booking windows.
The common bug is a loose partial match. A pattern like \d{1,2}:\d{2} can return true for bad values when the field should contain only a valid time. For validator code, anchor the expression and test range limits directly.
This workflow is JavaScript-first. It uses regular expressions as a browser-friendly format screen, then leaves scheduling rules, timezone labels, and date pairing to application code after the text shape is approved.
How to validate a time format regex
- Decide whether the field accepts 24-hour time, 12-hour AM/PM time, seconds, leading zeros, or business-hour limits.
- Use `^` and `$` when `regex.test(value)` must approve the whole time field instead of one time-like fragment.
- Keep the hour and minute ranges explicit so `24:00`, `12:60`, and one-digit minutes fail predictably.
- Test pasted whitespace, missing leading zeros, optional seconds, lowercase am/pm, and boundary values before shipping.
- After regex passes, parse the hour and minute in application code when scheduling, timezone, or calendar rules matter.
Time format expressions to test
24-hour HH:MM
^(?:[01]\d|2[0-3]):[0-5]\d$
Pass: 00:00, 09:30, 23:59
Fail: 24:00, 9:30, 12:60
Use when every value must have two hour digits, a colon, and two minute digits.
24-hour HH:MM with optional seconds
^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$
Pass: 00:00, 14:05:09, 23:59
Fail: 24:00, 14:05:70, 14:5:09
Good for log filters, schedule imports, and fields where seconds may appear but must stay in range.
12-hour time with AM or PM
^(?:0?[1-9]|1[0-2]):[0-5]\d\s?(?:[AaPp][Mm])$
Pass: 9:05 AM, 09:05AM, 12:30 pm
Fail: 00:30 PM, 13:00 PM, 9:5 AM
Allows an optional leading zero and optional space before AM or PM while rejecting 00 and 13.
Business hours in 30-minute steps
^(?:(?:09|1[0-6]):(?:00|30)|17:00)$
Pass: 09:00, 12:30, 16:30, 17:00
Fail: 08:30, 17:30, 12:15
A stricter example for booking forms where only half-hour slots from 09:00 through 17:00 are valid.
Pass and fail samples for time validators
Should pass 24-hour
00:00, 09:30, 14:05, 23:59, 14:05:09
Should pass 12-hour
9:05 AM, 09:05AM, 12:00 pm
Should fail
24:00, 12:60, 14:05:70, 13:00 PM, time 09:30
Decide by rule
9:30, 09:30:00, 09:30 UTC, 17:30
Copy-ready JavaScript regex.test examples
Boolean regex.test time check
const timePattern = /^(?:[01]\d|2[0-3]):[0-5]\d$/;
const isValidTime = timePattern.test(value);Use a non-global anchored regex when the result should be a simple true or false field check.
Validate multiple submitted rows
const timePattern = /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/;
const invalidTimes = values.filter((value) => !timePattern.test(value.trim()));Trim only if production input also trims. Keep the tester samples aligned with the same normalization step.
Parse after the regex passes
const timePattern = /^(?:[01]\d|2[0-3]):[0-5]\d$/;
if (timePattern.test(value)) {
const [hour, minute] = value.split(':').map(Number);
const minutesAfterMidnight = hour * 60 + minute;
}Regex confirms shape and range. Scheduling, timezone, blackout windows, and date pairing still belong in code.
Avoid loose partial matches
const loose = /\d{1,2}:\d{2}/;
loose.test('meeting at 24:99'); // true, but the field is not validA loose expression can be useful for extraction, but it is too permissive for a time-format validator.
Time regex decisions to make first
- Does the value represent a time of day, a duration, or a schedule slot?
- Should one-digit hours like `9:30` pass, or must the field store `09:30`?
- Are seconds required, optional, or forbidden?
- Should lowercase `am` and `pm` pass, or should the UI normalize casing first?
- Can timezone labels such as `UTC`, `PST`, or offsets appear in the same field?
- Will backend validation use the exact same rule as the JavaScript check?
Test a time format regex now
Open the live tester with a 24-hour time validator and mixed samples already loaded. The failing rows are included to expose out-of-range hours, invalid minutes, one-digit hours, and bad seconds.
Test Time Format RegexRelated Pages on Regex Tester
Open the live time regex tester
Load a JavaScript time-format pattern, flags, and pass/fail samples in the browser tester.
JavaScript regex validator
Build full-field validation expressions with anchors, examples, and JavaScript flag checks.
JavaScript regex.test validator
Check boolean validator behavior before using RegExp.test() in form code.
JavaScript regex.test generator
Turn time-format rules into a small expression and sample bank.
JavaScript regex flags tester
Confirm why validator patterns should avoid global and sticky flags in production checks.
Date format regex testing
Compare time-format checks with date-shape validation and fixture-driven testing.
JavaScript regex examples
Review copy-ready JavaScript expressions after validating behavior online.
Regex cheat sheet
Reference anchors, alternation, groups, character classes, and quantifiers.
JavaScript Time Regex FAQ
What is a JavaScript regex time format validator?
It is an anchored regular expression used with the JavaScript RegExp engine to decide whether a time string matches an accepted format, such as 24-hour HH:MM or 12-hour AM/PM.
Can regex prove that a scheduled time is valid?
Regex can validate the text shape and simple hour or minute ranges. It cannot decide timezone behavior, calendar availability, daylight saving changes, or booking rules.
Should a time validator use the global flag with regex.test?
No for normal field validation. A global regex changes `lastIndex` between calls, which can make repeated `.test()` checks unreliable. Use a non-global anchored regex in code.
Should 24:00 pass a 24-hour time regex?
Most time-of-day fields reject `24:00` and accept `00:00` through `23:59`. If your product treats `24:00` as end-of-day notation, make that an explicit business rule and test it separately.