How to compare two TimeOfDay using showTimePicker in Flutter?

141 Views Asked by At

I have a showTimePicker in my app and I want to compare between the picked time and the current real time.

I tried to used DateTime.now() and compare it to the value that was picked but the value is TimeOfDay and also DateTime.now() has year,month, day.. etc.

The scenario I want is that when a user picks a time after choosing a date I want my app to check if this time is overdue or not.

1

There are 1 best solutions below

0
On

I have write a extension for TimeOfDay, I think you can use func on this extension.

import 'package:flutter/material.dart';

extension TimeOfDayExtension on TimeOfDay {
  bool isBefore(TimeOfDay other) {
    return (60 * hour + minute) < (60 * other.hour + other.minute);
  }

  bool theSame(TimeOfDay other) {
    return (60 * hour + minute) == (60 * other.hour + other.minute);
  }

  bool isBeforeNow() {
    return isBefore(TimeOfDay.fromDateTime(DateTime.now()));
  }

  bool theSameNow() {
    return theSame(TimeOfDay.fromDateTime(DateTime.now()));
  }

  bool isBeforeDate(DateTime date) {
    return isBefore(TimeOfDay.fromDateTime(date));
  }

  bool theSameDate(DateTime date) {
    return theSame(TimeOfDay.fromDateTime(date));
  }
}