Convert timestamps to relative time phrases like '3 hours ago'
pip install philiprehberger-timeagoConvert timestamps to relative time phrases like "3 hours ago".
pip install philiprehberger-timeago
from philiprehberger_timeago import timeago, timedelta_human
from datetime import datetime, timedelta, timezone
now = datetime.now(timezone.utc)
timeago(now - timedelta(seconds=30)) # "30 seconds ago"
timeago(now - timedelta(hours=3)) # "3 hours ago"
timeago(now - timedelta(days=1)) # "yesterday"
timeago(now + timedelta(days=7)) # "in 1 week"
# Unix timestamps
timeago(1709913600)
# Duration formatting
timedelta_human(timedelta(hours=3, minutes=25)) # "3 hours, 25 minutes"
| Function / Class | Description |
|---|---|
timeago(dt, now=None) | Relative time phrase from datetime, date, or Unix timestamp |
timedelta_human(td) | Format a timedelta as readable duration |
pip install -e .
python -m pytest tests/ -v
If you find this project useful: