20

TimeOfDay文档没有比较运算符,原始比较不起作用。我现在能想到的唯一解决方案是转换TimeOfDayDateTime并使用DateTime差异方法。

有没有人有更好的解决方案?

4

7 回答 7

35

将其转换为双精度然后进行比较。

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

于 2019-02-12T00:24:37.800 回答
7

感谢@Lucas 的想法,您可以计算小时分钟

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');
于 2019-10-12T07:09:04.187 回答
3
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;
  }
}
于 2020-12-16T07:13:50.127 回答
3

我通过将两个值转换为分钟数并比较它们来计算差异:)

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;
于 2020-04-10T01:02:33.700 回答
1
TimeOfDay n = TimeOfDay.now();
int nowSec = (n.hour * 60 + n.minute) * 60;
int veiSec = (t.hour * 60 + t.minute) * 60;
int dif = veiSec - nowSec;
于 2021-06-11T18:42:55.097 回答
0

我不认为这是可能的。您也可以.subtract在 DateTime 中使用.difference

于 2019-02-11T22:32:06.660 回答
0
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`
      ),
    ),
  ),
于 2020-09-06T06:27:04.783 回答