BytePane

Relative Date Regex Pattern

Matches relative time expressions like "5 minutes ago" or "3 months from now" for human-readable timestamps.

Date & Time
Intermediate
Difficulty
Universal
Language
i
Flags
// Regular Expression
/^\d+\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now)$/i

Live Regex Tester

Pattern Breakdown

^\d+\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now)$
Character class [ ]
Group ( )
Quantifier { }
Anchor ^ $
Repetition * + ?
Escape \
Alternation |
Any char .

Code Examples

JavaScript

const regex = /^\d+\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now)$/i;
const test = "5 days ago";
console.log(regex.test(test)); // true

// Extract matches
const matches = test.match(regex);
console.log(matches);

Python

import re

pattern = r'^\d+\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now)$'
test = "5 days ago"
match = re.search(pattern, test, re.IGNORECASE)
print(match)  # Found!

Go

package main

import (
    "fmt"
    "regexp"
)

func main() {
    re := regexp.MustCompile(`^\d+\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now)$`)
    fmt.Println(re.MatchString("5 days ago")) // true
}

Common Use Cases

Natural language date parsingsocial media timestampsUX

Match Examples

InputResult
5 days agoMatch
yesterdayNo Match

About the Relative Date Regex

Matches relative time expressions like "5 minutes ago" or "3 months from now" for human-readable timestamps.

Regular expressions (regex) are powerful pattern matching tools used across virtually all programming languages. The relative date pattern is classified as intermediate difficulty in the date & time category. It works in all major programming languages.

When using this regex, always consider edge cases and test thoroughly with real-world data. Use the interactive tester above to validate the pattern against your specific inputs before deploying to production.

Need More Regex Patterns?

Browse our complete library of 100+ regex patterns with interactive testers.

Frequently Asked Questions

What is the Relative Date regex pattern?

Matches relative time expressions like "5 minutes ago" or "3 months from now" for human-readable timestamps.

How do I use the Relative Date regex?

Use the pattern /^\d+\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now)$/i in your code. In JavaScript: new RegExp('^\d+\s+(second|minute|hour|day|week|month|year)s?\s+(ago|from now)$', 'i'). Test it above with your own input.

What does this Relative Date regex match?

This pattern matches: "5 days ago". It does NOT match: "yesterday". Natural language date parsing, social media timestamps, UX.

Is the Relative Date regex beginner-friendly?

This pattern is rated Intermediate. It uses some advanced features like character classes and quantifiers.

What languages support the Relative Date regex?

This pattern works in all major programming languages including JavaScript, Python, Java, C#, Go, Ruby, PHP, and more. Syntax may vary slightly between regex engines.

Can I modify the Relative Date regex for my use case?

Yes! Use the interactive tester above to modify the pattern and test with your own data. Common modifications include making it case-insensitive (add 'i' flag), matching globally (add 'g' flag), or adjusting character classes.

Related Tools