DateTimeFormatting extension
Human-friendly timestamp formatting extension on nullable DateTime.
The output adapts automatically based on how far in the past this is:
| Age | Example output |
|---|---|
| < 1 minute | Just now |
| < 1 hour | 45m ago |
| Today | 02:30 PM |
| Yesterday | Yesterday, 02:30 PM |
| 2–6 days ago (same wk) | Friday, 02:30 PM |
| Same year, > 1 week | 15 Jan, 02:30 PM |
| Different year | 15 Jan 2024, 02:30 PM |
| null | Unknown time |
Basic usage
import 'package:flutter_timeago_pro/flutter_timeago_pro.dart';
Text(notification.createdAt.toTimeagoFormat());
Hide the time portion
post.publishedAt.toTimeagoFormat(showTimeForOveraged: false);
// → "Friday" / "15 Jan" / "15 Jan 2024"
Custom locale / i18n
const id = TimestampLocale(
justNow: 'Baru saja',
yesterday: 'Kemarin',
minutesAgoSuffix: 'm lalu',
unknownTime: 'Waktu tidak diketahui',
);
dateTime.toTimeagoFormat(locale: id);
Custom time format
dateTime.toTimeagoFormat(timePattern: 'HH:mm'); // 24-hour
- on
Methods
-
toTimeagoFormat(
{bool showTimeForOveraged = true, TimestampLocale locale = const TimestampLocale(), String timePattern = 'hh:mm a', DateTime? referenceTime, Duration timeagoLimit = const Duration(hours: 1)}) → String -
Available on DateTime?, provided by the DateTimeFormatting extension
Returns a human-friendly notification timestamp string relative to now.