mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2025-05-28 10:42:24 -04:00
41 lines
1.1 KiB
Dart
41 lines
1.1 KiB
Dart
import 'package:flutter_translate/flutter_translate.dart';
|
|
import 'package:intl/intl.dart';
|
|
|
|
class DateFormatter {
|
|
DateFormatter();
|
|
|
|
String chatDateTimeFormat(DateTime dateTime) {
|
|
final now = DateTime.now();
|
|
|
|
final justNow = now.subtract(const Duration(minutes: 1));
|
|
|
|
final localDateTime = dateTime.toLocal();
|
|
|
|
if (!localDateTime.difference(justNow).isNegative) {
|
|
return translate('date_formatter.just_now');
|
|
}
|
|
|
|
final roughTimeString = DateFormat.jm().format(dateTime);
|
|
|
|
if (localDateTime.day == now.day &&
|
|
localDateTime.month == now.month &&
|
|
localDateTime.year == now.year) {
|
|
return roughTimeString;
|
|
}
|
|
|
|
final yesterday = now.subtract(const Duration(days: 1));
|
|
|
|
if (localDateTime.day == yesterday.day &&
|
|
localDateTime.month == now.month &&
|
|
localDateTime.year == now.year) {
|
|
return translate('date_formatter.yesterday');
|
|
}
|
|
|
|
if (now.difference(localDateTime).inDays < 4) {
|
|
final weekday = DateFormat(DateFormat.WEEKDAY).format(localDateTime);
|
|
|
|
return '$weekday, $roughTimeString';
|
|
}
|
|
return '${DateFormat.yMd().format(dateTime)}, $roughTimeString';
|
|
}
|
|
}
|