datetimeflutterdarttimeofday

Find difference between to times in TimeOfDay class like (10:50 - 08:00 = 02:50 )


I should have two times, one is the current time and the other I get it from time picker as shown below.

How can I get the difference between them?

Code


Solution

  • You can use either of these two.

    int getMinutesDiff(TimeOfDay tod1, TimeOfDay tod2) {
      return (tod1.hour * 60 + tod1.minute) - (tod2.hour * 60 + tod2.minute);
    }
    
    TimeOfDay getTimeOfDayDiff(TimeOfDay tod1, TimeOfDay tod2) {
      var minutes = (tod1.hour * 60 + tod1.minute) - (tod2.hour * 60 + tod2.minute);
    
      return TimeOfDay(hour: minutes ~/ 60, minute: minutes % 60);
    }