Dart/Flutter How to compare two TimeOfDay times?

纵然是瞬间 提交于 2020-01-02 10:22:55

问题


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?


回答1:


Convert it to a double then compare.

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




回答2:


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




回答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 = _doubleOpenTime - _doubleNowTime;

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

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


来源:https://stackoverflow.com/questions/54639993/dart-flutter-how-to-compare-two-timeofday-times

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!