【问题标题】:Dart/Flutter How to compare two TimeOfDay times?Dart/Flutter 如何比较两个 TimeOfDay 时间?
【发布时间】:2026-02-16 23:35:01
【问题描述】:

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

谁有更好的解决方案?

【问题讨论】:

    标签: datetime dart flutter timeofday


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      感谢@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');
      

      【讨论】:

        【解决方案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;
          }
        }
        

        【讨论】:

          【解决方案4】:

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

          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;
          

          【讨论】:

            【解决方案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;
            

            【讨论】:

              【解决方案6】:

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

              【讨论】:

                【解决方案7】:
                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`
                      ),
                    ),
                  ),
                

                【讨论】: