Time with Fractional Seconds and Timezone Regex for JavaScript
/^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(?:\.(\d{1,9}))?(?:Z|([+-])(2[0-3]|[01][0-9]):([0-5][0-9]))?$/What this pattern does
This page provides a comprehensive, battle-tested regular expression for matching time with fractional seconds and timezone, ported and verified for JavaScript. A rigorously tested regex reduces debugging time and protects your application from edge-case failures. The snippet below is ready to drop into your JavaScript project — whether you're validating in an Express middleware, a Next.js API route, or a client-side form.
Javascript Implementation
// Time with Fractional Seconds and Timezone
// ReDoS-safe | RegexVault — Localization > Time Formats
const timeWithFractionalSecondsAndTimezoneRegex = /^(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(?:\.(\d{1,9}))?(?:Z|([+-])(2[0-3]|[01][0-9]):([0-5][0-9]))?$/;
function validateTimeWithFractionalSecondsAndTimezone(input: string): boolean {
return timeWithFractionalSecondsAndTimezoneRegex.test(input);
}
// Example
console.log(validateTimeWithFractionalSecondsAndTimezone("12:30:45")); // trueTest Cases
Matches (Valid) | Rejects (Invalid) |
|---|---|
12:30:45 | 24:00:00Z |
12:30:45Z | 12:60:00Z |
12:30:45.123 | 12:30:45+25:00 |
12:30:45.123456789Z | 12:30:45.1234567890 |
12:30:45+08:00 | — |
00:00:00-05:30 | — |
When to use this pattern
This pattern is drawn from the Localization > Time Formats category and carries a ReDoS-safe certification. That matters for JavaScript developers because especially critical in long-running Node.js event loops where a ReDoS vulnerability can block the entire process. RegexVault audits patterns against known backtracking attack vectors, ensuring you have the necessary context before using this regex in a high-stakes production environment.
Common Pitfalls
India Standard Time (+05:30), Newfoundland (-03:30), Iran (+03:30), and Australia Central (+09:30) use 30-minute offsets. Nepal (+05:45) uses a 45-minute offset. Pattern allows all combinations.
Technical Notes
Capture groups: 1=hour, 2=minute, 3=second, 4=fractional, 5=tz sign, 6=tz hour, 7=tz minute. Z = UTC. Fractional seconds up to 9 places (nanoseconds). Half-hour timezones (+05:30 India, +09:30 Australia) are supported.
Have a pattern that belongs in the vault?
Submit it for review — community-verified patterns get credited to your GitHub handle. Free submissions join the queue. Priority review available for $15.
Submit a Pattern