Time Ago Formatter
Convert dates and timestamps to relative time strings like '3 days ago' or 'in 2 months'.
About this tool
The Time Ago Formatter converts absolute dates and timestamps into relative time descriptions that humans naturally understand, like '3 days ago' or 'in 2 months'. This format is especially useful in chat applications, social media, activity feeds, and project management tools where showing when something happened matters more than displaying the exact datetime.
Simply enter any date or timestamp into the tool, and it instantly displays how long ago that moment was or how far in the future it lies. The formatter intelligently handles different time scales—seconds, minutes, hours, days, months, and years—automatically selecting the most meaningful unit for readability.
This tool is invaluable for developers building real-time applications, content creators managing posts and updates, and anyone who wants to understand time differences at a glance without doing mental math.
Frequently Asked Questions
Code Implementation
from datetime import datetime, timezone
def time_ago(past: datetime, now: datetime = None) -> str:
"""Return human-readable relative time string."""
if now is None:
now = datetime.now(timezone.utc)
diff = now - past
total_seconds = int(diff.total_seconds())
if total_seconds < 0:
return "in the future"
minutes = total_seconds // 60
hours = minutes // 60
days = hours // 24
months = days // 30
years = days // 365
if total_seconds < 60:
return f"{total_seconds} second{'s' if total_seconds != 1 else ''} ago"
elif minutes < 60:
return f"{minutes} minute{'s' if minutes != 1 else ''} ago"
elif hours < 24:
return f"{hours} hour{'s' if hours != 1 else ''} ago"
elif days < 30:
return f"{days} day{'s' if days != 1 else ''} ago"
elif months < 12:
return f"{months} month{'s' if months != 1 else ''} ago"
else:
return f"{years} year{'s' if years != 1 else ''} ago"
# Examples
from datetime import timedelta
now = datetime(2025, 6, 15, 12, 0, 0, tzinfo=timezone.utc)
print(time_ago(now - timedelta(seconds=30), now)) # 30 seconds ago
print(time_ago(now - timedelta(minutes=45), now)) # 45 minutes ago
print(time_ago(now - timedelta(days=3), now)) # 3 days ago
print(time_ago(now - timedelta(days=400), now)) # 1 year agoComments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.