'Dart/Flutter How to compare two TimeOfDay times?

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:[1]

Convert it to a double then compare.

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

Solution 2:[2]

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 3:[3]

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 4:[4]

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 5:[5]

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 6:[6]

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 7:[7]

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

Solution 8:[8]

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`
      ),
    ),
  ),

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Lucas
Solution 2 Akbar Pulatov
Solution 3 matthias_code
Solution 4 Matias de Andrea
Solution 5 Zoul Barizi
Solution 6 Shailandra Rajput
Solution 7 Fellipe Malta
Solution 8 David Buck