Skip to content

Commit 57e03dc

Browse files
committed
feat: add relative time formatter
- Formats DateTime to relative time - Uses timeago package - Handles null DateTime
1 parent 3309631 commit 57e03dc

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/shared/utils/date_formatter.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:timeago/timeago.dart' as timeago;
3+
4+
/// Formats the given [dateTime] into a relative time string
5+
/// (e.g., "5m ago", "Yesterday", "now").
6+
///
7+
/// Uses the current locale from [context] to format appropriately.
8+
/// Returns an empty string if [dateTime] is null.
9+
String formatRelativeTime(BuildContext context, DateTime? dateTime) {
10+
if (dateTime == null) {
11+
return '';
12+
}
13+
final locale = Localizations.localeOf(context).languageCode;
14+
return timeago.format(dateTime, locale: locale);
15+
}

0 commit comments

Comments
 (0)