Datetime 飞镖/颤振如何比较两个时间段?

Datetime 飞镖/颤振如何比较两个时间段?,datetime,dart,flutter,timeofday,Datetime,Dart,Flutter,Timeofday,TimeOfDay文档没有比较运算符,基本比较不起作用。我现在能想到的唯一解决方案是将TimeOfDay转换为DateTime,并使用DateTime的差分方法 有谁有更好的解决方案吗?我认为这是不可能的。您可以在DateTime中使用.subtract,也可以使用.difference将其转换为双精度,然后进行比较 double toDouble(TimeOfDay myTime)=>myTime.hour+myTime.minute/60.0感谢@Lucas idea,您可以通过 我通过将两

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


有谁有更好的解决方案吗?

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

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


double toDouble(TimeOfDay myTime)=>myTime.hour+myTime.minute/60.0
感谢@Lucas idea,您可以通过


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


我计算了两次之间的当前时间:

DateTime before;
DateTime later;
before = DateTime.now();
later = DateTime.now();
print(before.difference(later));
您可以计算到毫秒:

print(before.difference(Later).inMilliseconds);
TimeOfDayExtension on TimeOfDay{
int compareTo(其他日期的时间){
如果(this.hourother.hour)返回1;
如果(this.minuteother.minute)返回1;
返回0;
}
}
DateTime before;
DateTime later;
before = DateTime.now();
later = DateTime.now();
print(before.difference(later));
print(before.difference(Later).inMilliseconds);
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`
      ),
    ),
  ),
extension TimeOfDayExtension on TimeOfDay {
  int compareTo(TimeOfDay other) {
    if (this.hour < other.hour) return -1;
    if (this.hour > other.hour) return 1;
    if (this.minute < other.minute) return -1;
    if (this.minute > other.minute) return 1;
    return 0;
  }
}
TimeOfDay n = TimeOfDay.now();
int nowSec = (n.hour * 60 + n.minute) * 60;
int veiSec = (t.hour * 60 + t.minute) * 60;
int dif = veiSec - nowSec;