TimeOfDay documentation has no comparison operator and primitive comparison does not work. My only solution that I can thinking of right now is to convert TimeOfDay to DateTime and use DateTime's difference method.

Does anyone have a better solution?


Solution 1: Fellipe Malta

I don't think this is possible. You can use .subtract in DateTime as also .difference


Solution 2: Lucas

Convert it to a double then compare.

double toDouble(TimeOfDay myTime) => myTime.hour + myTime.minute/60.0


Solution 3: สรรพสิทธิ์ ศรีแสง

Thanks from @Lucas idea, you can calculate hour and minute by

TimeOfDay yourTime ; 
TimOfDay nowTime = TimeOfDay.now()

double _doubleYourTime = yourTime.hour.toDouble() +
            (yourTime.minute.toDouble() / 60);
double _doubleNowTime = nowTime.hour.toDouble() +
            (nowTime.minute.toDouble() / 60);

double _timeDiff = _doubleYourTime - _doubleNowTime;

double _hr = _timeDiff.truncate();
double _minute = (_timeDiff - _timeDiff.truncate()) * 60;

print('Here your Happy $_hr Hour and also $_minute min');


Solution 4: matthias_code

I calculated the difference by turning both values into minute-counts, and comparing those :)

TimeOfDay now = TimeOfDay.now();
int nowInMinutes = now.hour * 60 + now.minute;

TimeOfDay testDate = TimeOfDay(hour: 2, minute: 20);
int testDateInMinutes = testDate.hour * 60 + testDate.minute;


Solution 5: Kalyan Biswas

Card(
child: ListTile(
onTap: () {
showTimePicker(
context: context,
initialTime: TimeOfDay.now(),
).then((TimeOfDay time) {
double _doubleyourTime =
time.hour.toDouble() + (time.minute.toDouble() /60);
double _doubleNowTime = TimeOfDay.now().hour.toDouble() +
(TimeOfDay.now().minute.toDouble() / 60);enter code here
if (_doubleyourTime > _doubleNowTime) {
print('correct format')
});
} else {
print('Sorry You can not set the time')

      }
    });
  },
  //dense: true,
  leading: Icon(Icons.timer),
  title: Text(
    'Today On Time',`enter code here`
  ),
),

),


Solution 6: MichaelMitchell

extension TimeOfDayExtension on TimeOfDay {
int compareTo(TimeOfDay other) {
if (hour < other.hour) return -1;
if (hour > other.hour) return 1;
if (minute < other.minute) return -1;
if (minute > other.minute) return 1;
return 0;
}
}


Solution 7: Zoul Barizi

TimeOfDay n = TimeOfDay.now();
int nowSec = (n.hour  60 + n.minute)  60;
int veiSec = (t.hour  60 + t.minute)  60;
int dif = veiSec - nowSec;


Solution 8: Shailandra Rajput

You can use this method. Where you have to provide starttime and endTime in TimesofDay format.

getTime(startTime, endTime) {
  bool result = false;
  int startTimeInt = (startTime.hour * 60 + startTime.minute) * 60;
  int EndTimeInt = (endTime.hour * 60 + endTime.minute) * 60;
  int dif = EndTimeInt - startTimeInt;

  if (EndTimeInt > startTimeInt) {
    result = true;
  } else {
    result = false;
  }
  return result;
}

LikeThis

getTime(v1, v2);


Solution 9: Japsimrans13

We can actually use the subtract operator.

Code to make magic happen :

Here I wanted to get the difference in time after the user selects the time (in TimeOfDay format) using showTimePicker()

// current time will be used to find the difference between the time selected by the user.
TimeOfDay _cur_time = TimeOfDay(hour: DateTime.now().hour, minute: DateTime.now().minute);

// scheduled time will be updated as soon as the user inputs a new time using the showTimePicker() function available in Flutter Material librabry.
TimeOfDay _scheduled_time = TimeOfDay(hour: DateTime.now().hour, minute: DateTime.now().minute);

// toDouble Function to convert time to double so that we can compare time and check that the time selected is greater than current time.
      double toDouble(TimeOfDay myTime) => myTime.hour + myTime.minute / 60.0;

  void _selectTime() async {
Flutter Material widget to select time.
final TimeOfDay? newTime = await showTimePicker(
  context: context,
  initialTime: _scheduled_time,
);
//Check if the selected time is greater than the cur time
if (toDouble(newTime!) > toDouble(_cur_time)) {
  setState(() {
    _scheduled_time = newTime;
  });
}
  }
Function to get the difference between cur time and selected time.
      Duration _getDelayedDuration(){
        var hourDelay = _scheduled_time.hour - _cur_time.hour;
        print(hourDelay);
        var minuteDelay = _scheduled_time.minute - _cur_time.minute;
        print(minuteDelay);
        return Duration(hours: hourDelay, minutes: minuteDelay);
      }