isDateRange method
/....... for Ranges .......//// Date Ranges (Yesterday, Today, Tomorrow, Last 7 Days, etc.) Easy access to common date ranges for filtering data. Example: bool isToday = IntlDateHelper.isToday(DateTime.now()); Checks if the date matches the given DateRangeType Supports: today, yesterday, tomorrow, thisWeek, last7Days, leapYear, thisMonth, lastMonth, nextMonth, thisYear, lastYear, nextYear
Implementation
bool isDateRange(DateRangeType type) {
final date = this;
DateTime now = DateTime.now();
switch (type) {
case DateRangeType.today:
return date.year == now.year &&
date.month == now.month &&
date.day == now.day;
case DateRangeType.yesterday:
DateTime yesterday = now.subtract(Duration(days: 1));
return date.year == yesterday.year &&
date.month == yesterday.month &&
date.day == yesterday.day;
case DateRangeType.tomorrow:
DateTime tomorrow = now.add(Duration(days: 1));
return date.year == tomorrow.year &&
date.month == tomorrow.month &&
date.day == tomorrow.day;
case DateRangeType.thisWeek:
DateTime startOfWeek =
DateTime(now.year, now.month, now.day - now.weekday);
DateTime endOfWeek =
DateTime(now.year, now.month, now.day + (7 - now.weekday));
return date.isAfter(startOfWeek) && date.isBefore(endOfWeek);
case DateRangeType.last7Days:
DateTime last7Days = now.subtract(Duration(days: 7));
return date.isAfter(last7Days) && date.isBefore(now);
case DateRangeType.leapYear:
final year = date.year;
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
case DateRangeType.thisMonth:
return date.year == now.year && date.month == now.month;
case DateRangeType.lastMonth:
DateTime lastMonth = DateTime(now.year, now.month - 1);
return date.year == lastMonth.year && date.month == lastMonth.month;
case DateRangeType.nextMonth:
DateTime nextMonth = DateTime(now.year, now.month + 1);
return date.year == nextMonth.year && date.month == nextMonth.month;
case DateRangeType.thisYear:
return date.year == now.year;
case DateRangeType.lastYear:
return date.year == now.year - 1;
case DateRangeType.nextYear:
return date.year == now.year + 1;
}
}