Home > Software engineering >  Flutter check for month changes in dates
Flutter check for month changes in dates

Time:10-10

How can I check for month-to-month changes between dates? For example:

  DateTime a = DateTime(2022, 10, 01);
  DateTime b = DateTime(2022, 09, 22);
  if (...)
  print(isChange);

Output: true.

If:

  DateTime a = DateTime(2022, 09, 24);
  DateTime b = DateTime(2022, 09, 22);
  if (...)
  print(isChange);

Output: false.

CodePudding user response:

You can use this function:

bool isChange(DateTime a, DateTime b) {
   return a.month != b.month || a.year != b.year;
}

and use it like this:

DateTime a = DateTime(2022, 09, 24);
DateTime b = DateTime(2022, 09, 22);

print("ischange = ${isChange(a, b)}"); //ischange = false

CodePudding user response:

You can simply compare the month and year property

if (a.month == b.month && a.year == b.year) {
    // ...something else you want to do...
    print(false)
} else {
    // ...something else you want to do...
    print(true)
}
  • Related