Home > front end >  Compare minutes in Flutter
Compare minutes in Flutter

Time:05-28

I have the following function:

void compareTimes() {
int daysBetween(DateTime from, DateTime to) {
    from = DateTime(from.year, from.month, from.day);
    to = DateTime(to.year, to.month, to.day);
  return (to.difference(from).inDays).round();
}

  final date1 = DateTime.parse("2022-05-27 23:00:00");
  final date2 = DateTime.parse("2022-05-28 23:00:00");
  final difference = daysBetween(date1, date2);
  print(difference);
}

With it I can get the DAYS difference between date1 and date2.

But, I would like to get the minutes difference between these dates, example: "2022-05-27 23:00:00" and "2022-05-27 23:10:00", it's 10 minutes difference, any ideas?

CodePudding user response:

Just use the Duration's property inMinutes:

void compareTimes() {
  int minutesBetween(DateTime from, DateTime to) =>
      to.difference(from).inMinutes;

  final date1 = DateTime.parse("2022-05-27 23:00:00");
  final date2 = DateTime.parse("2022-05-27 23:10:00");
  final difference = minutesBetween(date1, date2);
  print(difference); // 10
}

CodePudding user response:

you can use inMinutes

void compareTimes() {
int daysBetween(DateTime from, DateTime to) {
    from = DateTime(from.year, from.month, from.day);
    to = DateTime(to.year, to.month, to.day);
  return (to.difference(from).inMinutes).round();
}

  final date1 = DateTime.parse("2022-05-27 23:00:00");
  final date2 = DateTime.parse("2022-05-28 23:00:00");
  final difference = daysBetween(date1, date2);
  print(difference);
}
  • Related