Regex to Match Relative Date Expressions
Match relative date expressions like '5 minutes ago', '3 days from now', or '2 weeks later'. Captures the quantity, unit, and direction. Free regex tester.
Regular Expression
/(\d+)\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now|later|earlier)/gi
Token Breakdown
| Token | Description |
|---|---|
| ( | Start of capturing group |
| \d | Matches any digit (0-9) |
| + | Matches the preceding element one or more times (greedy) |
| ) | End of group |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| ( | Start of capturing group |
| s | Matches the literal character 's' |
| e | Matches the literal character 'e' |
| c | Matches the literal character 'c' |
| o | Matches the literal character 'o' |
| n | Matches the literal character 'n' |
| d | Matches the literal character 'd' |
| | | Alternation — matches the expression before OR after the pipe |
| m | Matches the literal character 'm' |
| i | Matches the literal character 'i' |
| n | Matches the literal character 'n' |
| u | Matches the literal character 'u' |
| t | Matches the literal character 't' |
| e | Matches the literal character 'e' |
| | | Alternation — matches the expression before OR after the pipe |
| h | Matches the literal character 'h' |
| o | Matches the literal character 'o' |
| u | Matches the literal character 'u' |
| r | Matches the literal character 'r' |
| | | Alternation — matches the expression before OR after the pipe |
| d | Matches the literal character 'd' |
| a | Matches the literal character 'a' |
| y | Matches the literal character 'y' |
| | | Alternation — matches the expression before OR after the pipe |
| w | Matches the literal character 'w' |
| e | Matches the literal character 'e' |
| e | Matches the literal character 'e' |
| k | Matches the literal character 'k' |
| | | Alternation — matches the expression before OR after the pipe |
| m | Matches the literal character 'm' |
| o | Matches the literal character 'o' |
| n | Matches the literal character 'n' |
| t | Matches the literal character 't' |
| h | Matches the literal character 'h' |
| | | Alternation — matches the expression before OR after the pipe |
| y | Matches the literal character 'y' |
| e | Matches the literal character 'e' |
| a | Matches the literal character 'a' |
| r | Matches the literal character 'r' |
| ) | End of group |
| s | Matches the literal character 's' |
| ? | Makes the preceding element optional (zero or one times) |
| \s | Matches any whitespace character (space, tab, newline) |
| + | Matches the preceding element one or more times (greedy) |
| ( | Start of capturing group |
| a | Matches the literal character 'a' |
| g | Matches the literal character 'g' |
| o | Matches the literal character 'o' |
| | | Alternation — matches the expression before OR after the pipe |
| f | Matches the literal character 'f' |
| r | Matches the literal character 'r' |
| o | Matches the literal character 'o' |
| m | Matches the literal character 'm' |
| Matches the literal character ' ' | |
| n | Matches the literal character 'n' |
| o | Matches the literal character 'o' |
| w | Matches the literal character 'w' |
| | | Alternation — matches the expression before OR after the pipe |
| l | Matches the literal character 'l' |
| a | Matches the literal character 'a' |
| t | Matches the literal character 't' |
| e | Matches the literal character 'e' |
| r | Matches the literal character 'r' |
| | | Alternation — matches the expression before OR after the pipe |
| e | Matches the literal character 'e' |
| a | Matches the literal character 'a' |
| r | Matches the literal character 'r' |
| l | Matches the literal character 'l' |
| i | Matches the literal character 'i' |
| e | Matches the literal character 'e' |
| r | Matches the literal character 'r' |
| ) | End of group |
Detailed Explanation
This regex matches natural language relative date and time expressions. Here is the token-by-token breakdown:
(\d+) — Capturing group 1 matches one or more digits representing the quantity. This captures the numeric value like 5 in 5 minutes ago or 30 in 30 days from now.
\s+ — Matches one or more whitespace characters between the number and the time unit.
(second|minute|hour|day|week|month|year) — Capturing group 2 matches the time unit. The alternation provides seven common time units from the smallest (second) to the largest (year).
s? — Optionally matches a trailing s for plural forms. This allows both singular (1 day) and plural (5 days) units.
\s+ — Matches one or more whitespace characters between the unit and the direction.
(ago|from now|later|earlier) — Capturing group 3 matches the direction indicator. ago and earlier indicate past dates, while from now and later indicate future dates.
The g flag enables global matching and the i flag makes the match case-insensitive. This pattern captures three groups: the numeric quantity, the time unit, and the direction, providing all information needed to calculate the absolute date.
Relative date expressions are commonly used in user interfaces, log filtering, search queries, and natural language date parsing. Examples include: 5 minutes ago, 3 days from now, 2 weeks later, and 1 year earlier. This pattern is useful for chat applications, content timestamps, scheduling interfaces, and search tools that accept natural language date queries.
Example Test Strings
| Input | Expected |
|---|---|
| 5 minutes ago | Match |
| 3 days from now | Match |
| 1 year later | Match |
| tomorrow | No Match |
| 2 weeks earlier | Match |
Try It — Interactive Tester
Match Highlighting(4 matches)
Matches & Capture Groups
81 charsFlags: giMatches: 4Ctrl+Shift+C to copy regex
Related Regex Patterns
Regex to Match ISO 8601 Duration Format
/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(?:T(?!$)(\d+H)?(\d+M)?(\d+(?:\.\d+)?S)?)?$/
Regex to Match YYYY-MM-DD Dates
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$/
Regex to Match ISO 8601 DateTime
/^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])T(?:[01]\d|2[0-3]):[0-5]\d:[0-5]\d(?:\.\d{1,3})?(?:Z|[+-](?:[01]\d|2[0-3]):[0-5]\d)$/
Regex to Match Timezone Offsets
/^(?:Z|[+-](?:0[0-9]|1[0-4]):[0-5][0-9])$/