getDaysDifference static method

int getDaysDifference(
  1. DateTime a,
  2. DateTime b
)

Returns count of days between two dates.

Time will be ignored, so for the dates (2020, 11, 18, 16, 50) and (2020, 11, 19, 10, 00) result will be 1.

Use this method for count days instead of a.difference(b).inDays, since it can return some unexpected result, because of daylight saving hour.

Implementation

static int getDaysDifference(DateTime a, DateTime b) {
  final straight = a.isBefore(b);
  final start = startOfDay(straight ? a : b);
  final end = startOfDay(straight ? b : a).add(const Duration(hours: 12));
  final diff = end.difference(start);
  return diff.inHours ~/ 24;
}