Jisho-Study-Tool/lib/services/datetime.dart

40 lines
943 B
Dart
Raw Normal View History

2022-01-25 20:44:00 +01:00
DateTime roundToDay(DateTime date) => DateTime(date.year, date.month, date.day);
bool dateIsEqual(DateTime date1, DateTime date2) =>
roundToDay(date1) == roundToDay(date2);
DateTime get today => roundToDay(DateTime.now());
DateTime get yesterday =>
roundToDay(DateTime.now().subtract(const Duration(days: 1)));
String formatTime(DateTime timestamp) {
final hours = timestamp.hour.toString().padLeft(2, '0');
final mins = timestamp.minute.toString().padLeft(2, '0');
return '$hours:$mins';
}
String formatDate(DateTime date) {
if (date == today) return 'Today';
if (date == yesterday) return 'Yesterday';
const List<String> monthTable = [
'Jan',
'Feb',
'Mar',
'Apr',
'May',
'Jun',
'Jul',
'Aug',
'Sep',
'Oct',
'Nov',
'Dec',
];
final int day = date.day;
final String month = monthTable[date.month - 1];
final int year = date.year;
return '$day. $month $year';
}