use extension methods for DateTime utilities
This commit is contained in:
parent
737656730a
commit
ac1458f6de
2 changed files with 21 additions and 19 deletions
|
@ -1,17 +1,3 @@
|
||||||
bool isAtSameYearAs(DateTime d1, DateTime d2) => d1 != null && d2 != null && d1.year == d2.year;
|
|
||||||
|
|
||||||
bool isAtSameMonthAs(DateTime d1, DateTime d2) => isAtSameYearAs(d1, d2) && d1.month == d2.month;
|
|
||||||
|
|
||||||
bool isAtSameDayAs(DateTime d1, DateTime d2) => isAtSameMonthAs(d1, d2) && d1.day == d2.day;
|
|
||||||
|
|
||||||
bool isToday(DateTime d) => isAtSameDayAs(d, DateTime.now());
|
|
||||||
|
|
||||||
bool isYesterday(DateTime d) => isAtSameDayAs(d, DateTime.now().subtract(const Duration(days: 1)));
|
|
||||||
|
|
||||||
bool isThisMonth(DateTime d) => isAtSameMonthAs(d, DateTime.now());
|
|
||||||
|
|
||||||
bool isThisYear(DateTime d) => isAtSameYearAs(d, DateTime.now());
|
|
||||||
|
|
||||||
String formatDuration(Duration d) {
|
String formatDuration(Duration d) {
|
||||||
String twoDigits(int n) {
|
String twoDigits(int n) {
|
||||||
if (n >= 10) return '$n';
|
if (n >= 10) return '$n';
|
||||||
|
@ -24,3 +10,19 @@ String formatDuration(Duration d) {
|
||||||
String twoDigitMinutes = twoDigits(d.inMinutes.remainder(Duration.minutesPerHour));
|
String twoDigitMinutes = twoDigits(d.inMinutes.remainder(Duration.minutesPerHour));
|
||||||
return '${d.inHours}:$twoDigitMinutes:$twoDigitSeconds';
|
return '${d.inHours}:$twoDigitMinutes:$twoDigitSeconds';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension ExtraDateTime on DateTime {
|
||||||
|
bool isAtSameYearAs(DateTime other) => this != null && other != null && this.year == other.year;
|
||||||
|
|
||||||
|
bool isAtSameMonthAs(DateTime other) => isAtSameYearAs(other) && this.month == other.month;
|
||||||
|
|
||||||
|
bool isAtSameDayAs(DateTime other) => isAtSameMonthAs(other) && this.day == other.day;
|
||||||
|
|
||||||
|
bool get isToday => isAtSameDayAs(DateTime.now());
|
||||||
|
|
||||||
|
bool get isYesterday => isAtSameDayAs(DateTime.now().subtract(const Duration(days: 1)));
|
||||||
|
|
||||||
|
bool get isThisMonth => isAtSameMonthAs(DateTime.now());
|
||||||
|
|
||||||
|
bool get isThisYear => isAtSameYearAs(DateTime.now());
|
||||||
|
}
|
||||||
|
|
|
@ -14,9 +14,9 @@ class DaySectionHeader extends StatelessWidget {
|
||||||
static DateFormat ymd = DateFormat.yMMMMd();
|
static DateFormat ymd = DateFormat.yMMMMd();
|
||||||
|
|
||||||
static String _formatDate(DateTime date) {
|
static String _formatDate(DateTime date) {
|
||||||
if (isToday(date)) return 'Today';
|
if (date.isToday) return 'Today';
|
||||||
if (isYesterday(date)) return 'Yesterday';
|
if (date.isYesterday) return 'Yesterday';
|
||||||
if (isThisYear(date)) return md.format(date);
|
if (date.isThisYear) return md.format(date);
|
||||||
return ymd.format(date);
|
return ymd.format(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,8 +37,8 @@ class MonthSectionHeader extends StatelessWidget {
|
||||||
static DateFormat ym = DateFormat.yMMMM();
|
static DateFormat ym = DateFormat.yMMMM();
|
||||||
|
|
||||||
static String _formatDate(DateTime date) {
|
static String _formatDate(DateTime date) {
|
||||||
if (isThisMonth(date)) return 'This month';
|
if (date.isThisMonth) return 'This month';
|
||||||
if (isThisYear(date)) return m.format(date);
|
if (date.isThisYear) return m.format(date);
|
||||||
return ym.format(date);
|
return ym.format(date);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue