Skip to content

Commit 826cda4

Browse files
committed
refactor: Date formatting utility class
- Added formatDate method - Encapsulated logic in class
1 parent ae36a8b commit 826cda4

File tree

1 file changed

+24
-10
lines changed

1 file changed

+24
-10
lines changed

lib/shared/utils/date_formatter.dart

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import 'package:flutter/material.dart';
2+
import 'package:intl/intl.dart';
23
import 'package:timeago/timeago.dart' as timeago;
34

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 '';
5+
/// A utility class for formatting dates and times.
6+
abstract final class DateFormatter {
7+
/// Formats the given [dateTime] into a relative time string
8+
/// (e.g., "5m ago", "Yesterday", "now").
9+
///
10+
/// Uses the current locale from [context] to format appropriately.
11+
/// Returns an empty string if [dateTime] is null.
12+
static String formatRelativeTime(BuildContext context, DateTime? dateTime) {
13+
if (dateTime == null) {
14+
return '';
15+
}
16+
final locale = Localizations.localeOf(context).languageCode;
17+
return timeago.format(dateTime, locale: locale);
18+
}
19+
20+
/// Formats the given [dateTime] into a short date string (e.g., "Jul 1, 2025").
21+
///
22+
/// Returns an empty string if [dateTime] is null.
23+
static String formatDate(DateTime? dateTime) {
24+
if (dateTime == null) {
25+
return '';
26+
}
27+
return DateFormat.yMMMd().format(dateTime);
1228
}
13-
final locale = Localizations.localeOf(context).languageCode;
14-
return timeago.format(dateTime, locale: locale);
1529
}

0 commit comments

Comments
 (0)