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

TokenDescription
(Start of capturing group
\dMatches any digit (0-9)
+Matches the preceding element one or more times (greedy)
)End of group
\sMatches any whitespace character (space, tab, newline)
+Matches the preceding element one or more times (greedy)
(Start of capturing group
sMatches the literal character 's'
eMatches the literal character 'e'
cMatches the literal character 'c'
oMatches the literal character 'o'
nMatches the literal character 'n'
dMatches the literal character 'd'
|Alternation — matches the expression before OR after the pipe
mMatches the literal character 'm'
iMatches the literal character 'i'
nMatches the literal character 'n'
uMatches the literal character 'u'
tMatches the literal character 't'
eMatches the literal character 'e'
|Alternation — matches the expression before OR after the pipe
hMatches the literal character 'h'
oMatches the literal character 'o'
uMatches the literal character 'u'
rMatches the literal character 'r'
|Alternation — matches the expression before OR after the pipe
dMatches the literal character 'd'
aMatches the literal character 'a'
yMatches the literal character 'y'
|Alternation — matches the expression before OR after the pipe
wMatches the literal character 'w'
eMatches the literal character 'e'
eMatches the literal character 'e'
kMatches the literal character 'k'
|Alternation — matches the expression before OR after the pipe
mMatches the literal character 'm'
oMatches the literal character 'o'
nMatches the literal character 'n'
tMatches the literal character 't'
hMatches the literal character 'h'
|Alternation — matches the expression before OR after the pipe
yMatches the literal character 'y'
eMatches the literal character 'e'
aMatches the literal character 'a'
rMatches the literal character 'r'
)End of group
sMatches the literal character 's'
?Makes the preceding element optional (zero or one times)
\sMatches any whitespace character (space, tab, newline)
+Matches the preceding element one or more times (greedy)
(Start of capturing group
aMatches the literal character 'a'
gMatches the literal character 'g'
oMatches the literal character 'o'
|Alternation — matches the expression before OR after the pipe
fMatches the literal character 'f'
rMatches the literal character 'r'
oMatches the literal character 'o'
mMatches the literal character 'm'
Matches the literal character ' '
nMatches the literal character 'n'
oMatches the literal character 'o'
wMatches the literal character 'w'
|Alternation — matches the expression before OR after the pipe
lMatches the literal character 'l'
aMatches the literal character 'a'
tMatches the literal character 't'
eMatches the literal character 'e'
rMatches the literal character 'r'
|Alternation — matches the expression before OR after the pipe
eMatches the literal character 'e'
aMatches the literal character 'a'
rMatches the literal character 'r'
lMatches the literal character 'l'
iMatches the literal character 'i'
eMatches the literal character 'e'
rMatches 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

InputExpected
5 minutes agoMatch
3 days from nowMatch
1 year laterMatch
tomorrowNo Match
2 weeks earlierMatch

Try It — Interactive Tester

//gi
gimsuy

Match Highlighting(4 matches)

5 minutes ago 3 days from now 1 year later tomorrow 2 weeks earlier

Matches & Capture Groups

#15 minutes agoindex 0
Group 1:5
Group 2:minute
Group 3:ago
#23 days from nowindex 14
Group 1:3
Group 2:day
Group 3:from now
#31 year laterindex 30
Group 1:1
Group 2:year
Group 3:later
#42 weeks earlierindex 52
Group 1:2
Group 2:week
Group 3:earlier
Pattern: 81 charsFlags: giMatches: 4

Ctrl+Shift+C to copy regex

Customize this pattern →