Home > Back-end >  Compare Hours in flutter
Compare Hours in flutter

Time:12-01

I'm facing troubles trying to compare times in flutter. I'm receiving a time : 09:30 I want to compare it with the current time and run some action.

Eg: received time from api (string) 09:30 current date : 2021-11-30 14:57:23

I want to compare 09:30 and 14:57 If 09:30 < 14:57 run some action

Thanks

CodePudding user response:

You can use this comparator function for that:

int compareHours(DateTime a, DateTime b) {
  var result = a.hour.compareTo(b.hour);
  if(result != 0) {
    return result;
  }
  
  result = a.minute.compareTo(b.minute);
  if(result != 0) {
    return result;
  }

  return a.second.compareTo(b.second);
}

CodePudding user response:

You can use the methods from DateTime class:

isAfter(), isBefore() and isAtSameMomentAs()

  • Related