TimeOfDay文档没有比较运算符,并且原始比较不起作用。我现在唯一能想到的解决方案是将TimeOfDay转换为DateTime并使用DateTime的差分方法。
有人有更好的解决方案吗?
答案 0 :(得分:1)
我通过将两个值都转换为分钟数,然后比较它们来计算差异:)
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;
答案 1 :(得分:0)
我认为这不可能。您也可以在DateTime中使用.subtract
答案 2 :(得分:0)
将其转换为双精度然后进行比较。
double toDouble(TimeOfDay myTime) => myTime.hour + myTime.minute/60.0
答案 3 :(得分:0)
由于@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 = _doubleOpenTime - _doubleNowTime;
double _hr = _timeDiff.truncate();
double _minute = (_timeDiff - _timeDiff.truncate()) * 60;
print('Here your Happy $_hr Hour and _minute min');
答案 4 :(得分:0)
您无法真正比较2个TimeOfDay
变量,因为您没有足够的信息。 Dart不知道您要比较哪个日期,月份和年份。如您所述,唯一的方法是将TimeOfDay
转换为DateTime
。
否则,您始终可以按照其他答案中的建议将TimeOfDay
转换为double
并计算差值。
答案 5 :(得分: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`
),
),
),
答案 6 :(得分:0)
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;
}
}
答案 7 :(得分: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;
答案 8 :(得分:-1)
我计算了两次之间的当前时间:
DateTime before;
DateTime later;
before = DateTime.now();
later = DateTime.now();
print(before.difference(later));
您可以计算到毫秒:
print(before.difference(Later).inMilliseconds);