'Flutter - check if the current time is in between a given hourly range

I want to check if the current time is between my opening time and my closing time, knowing that the close time can some times be 2 am and the opening time is 3 am, for example, I have been trying to handle this problem logically for 2 weeks now and I can't wrap my head around it, this is my best try yet:

  open = new DateTime(now.year, now.month, now.day, open.hour, open.minute);
  close = new DateTime(now.year, now.month, now.day, close.hour, close.minute);
  midnight = new DateTime(now.year, now.month, now.day, midnight.hour, midnight.minute);


  if(close.hour > midnight.hour && close.hour < open.hour){

   
    if(now.hour < midnight.hour){
      DateTime theClose = new DateTime(now.year, now.month, now.day + 1, close.hour, close.minute);

    

      if(now.isBefore(theClose) && now.isAfter(open)){
        sendIt(context, notes);
      }else{
    
        _showToast("this branch is closed right now");
      }

    }else{
      open = new DateTime(now.year, now.month, now.day - 1, open.hour, open.minute);

      if(now.isBefore(close) && now.isAfter(open)){
        sendIt(context, notes);
      }else{
  
        _showToast("this branch is closed right now");
      }

    }


  }else{


    if(now.isBefore(close) && now.isAfter(open)){
      sendIt(context, notes);

    }else{
 
      _showToast("this branch is closed right now");
    }

  }


Solution 1:[1]

 bool isValidTimeRange(TimeOfDay startTime, TimeOfDay endTime) {
    TimeOfDay now = TimeOfDay.now();
    return ((now.hour > startTime.hour) || (now.hour == startTime.hour && now.minute >= startTime.minute))
        && ((now.hour < endTime.hour) || (now.hour == endTime.hour && now.minute <= endTime.minute));
  }

Solution 2:[2]

As you have noticed, using DateTime in our case is not the best solution because it relies on the month/year/day. Instead, we can make use of the TimeOfDay class that does not rely on a specific day, but only on the time:

List<TimeOfDay> openingTimeRange = [TimeOfDay(hour: 2, minute: 30), TimeOfDay(hour: 15, minute: 45)]; // as an example
bool isOpen(List<TimeOfDay> openingTimeRange) {
   TimeOfDay now = TimeOfDay.now();
   return now.hour >= openingTimeRange[0].hour
      && now.minute >= openingTimeRange[0].minute
      && now.hour <= openingTimeRange[1].hour
      && now.minute <= openingTimeRange[1].minute;
}

Solution 3:[3]

I have a simple solution if your time format is in 24 hours. For that you don't require any external library.

bool _getStoreOpenStatus(String openTime, String closeTime) {
    bool result = false;

    DateTime now = DateTime.now();
    int nowHour = now.hour;
    int nowMin = now.minute;

    print('Now: H$nowHour M$nowMin');

    var openTimes = openTime.split(":");
    int openHour = int.parse(openTimes[0]);
    int openMin = int.parse(openTimes[1]);

    print('OpenTimes: H$openHour M$openMin');

    var closeTimes = closeTime.split(":");
    int closeHour = int.parse(closeTimes[0]);
    int closeMin = int.parse(closeTimes[1]);

    print('CloseTimes: H$closeHour M$closeMin');

    if(nowHour >= openHour && nowHour <= closeHour) {
      if(nowMin > openMin && nowMin < closeMin) result = true;
    }

    return result;
  }

Solution 4:[4]

If you use your time in the digital format [0..23] for hours, then you can convert the time to the amount of seconds that have past in that day. Do the same for the ranges you would like to check and see whether the current time past in seconds are between the two number ranges (in seconds):


TimeOfDay now = TimeOfDay.now(); // or DateTime object
TimeOfDay openingTime = TimeOfDay(hours: ??, minutes:??); // or leave as DateTime object
TimeOfDay closingTime = TimeOfDay(hours: ??, minutes:??); // or leave as DateTime object

int shopOpenTimeInSeconds = openingTime.hour * 60 + openingTime.minute;
int shopCloseTimeInSeconds = closingTime.hour * 60 + closingTime.minute;
int timeNowInSeconds = now.hour * 60 + now.minute;

if (shopOpenTimeInSeconds <= timeNowInSeconds &&
    timeNowInSeconds <= shopCloseTimeInSeconds) {
  // OPEN;
} else {
  // CLOSED;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chickin Nick
Solution 2 Stefano Amorelli
Solution 3 Dharman
Solution 4 Wian Snyman