【问题标题】:Flutter - check if the current time is in between a given hourly rangeFlutter - 检查当前时间是否在给定的每小时范围内
【发布时间】:2021-05-09 08:23:54
【问题描述】:

我想检查当前时间是否在我的开放时间和我的关闭时间之间,知道关闭时间有时可能是凌晨 2 点,而开放时间是凌晨 3 点,例如,我一直在尝试处理这个逻辑上的问题已经 2 周了,我无法解决这个问题,这是我最好的尝试:

  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");
    }

  }

【问题讨论】:

    标签: flutter dart time


    【解决方案1】:
    //checks if restaurant is open or closed 
    // returns true if current time is in between given timestamps
    //openTime HH:MMAM or HH:MMPM same for closedTime
    bool checkRestaurentStatus(String openTime, String closedTime) {
        //NOTE: Time should be as given format only
        //10:00PM
        //10:00AM
    
        // 01:60PM ->13:60
        //Hrs:Min
        //if AM then its ok but if PM then? 12+time (12+10=22)
    
        TimeOfDay timeNow = TimeOfDay.now();
        String openHr = openTime.substring(0, 2);
        String openMin = openTime.substring(3, 5);
        String openAmPm = openTime.substring(5);
        TimeOfDay timeOpen;
        if (openAmPm == "AM") {
          //am case
          if (openHr == "12") {
            //if 12AM then time is 00
            timeOpen = TimeOfDay(hour: 00, minute: int.parse(openMin));
          } else {
            timeOpen =
                TimeOfDay(hour: int.parse(openHr), minute: int.parse(openMin));
          }
        } else {
          //pm case
          if (openHr == "12") {
    //if 12PM means as it is
            timeOpen =
                TimeOfDay(hour: int.parse(openHr), minute: int.parse(openMin));
          } else {
    //add +12 to conv time to 24hr format
            timeOpen =
                TimeOfDay(hour: int.parse(openHr) + 12, minute: int.parse(openMin));
          }
        }
    
        String closeHr = closedTime.substring(0, 2);
        String closeMin = closedTime.substring(3, 5);
        String closeAmPm = closedTime.substring(5);
    
        TimeOfDay timeClose;
    
        if (closeAmPm == "AM") {
          //am case
          if (closeHr == "12") {
            timeClose = TimeOfDay(hour: 0, minute: int.parse(closeMin));
          } else {
            timeClose =
                TimeOfDay(hour: int.parse(closeHr), minute: int.parse(closeMin));
          }
        } else {
          //pm case
          if (closeHr == "12") {
            timeClose =
                TimeOfDay(hour: int.parse(closeHr), minute: int.parse(closeMin));
          } else {
            timeClose = TimeOfDay(
                hour: int.parse(closeHr) + 12, minute: int.parse(closeMin));
          }
        }
    
        int nowInMinutes = timeNow.hour * 60 + timeNow.minute;
        int openTimeInMinutes = timeOpen.hour * 60 + timeOpen.minute;
        int closeTimeInMinutes = timeClose.hour * 60 + timeClose.minute;
    
    //handling day change ie pm to am
        if ((closeTimeInMinutes - openTimeInMinutes) < 0) {
          closeTimeInMinutes = closeTimeInMinutes + 1440;
          if (nowInMinutes >= 0 && nowInMinutes < openTimeInMinutes) {
            nowInMinutes = nowInMinutes + 1440;
          }
          if (openTimeInMinutes < nowInMinutes &&
              nowInMinutes < closeTimeInMinutes) {
            return true;
          }
        } else if (openTimeInMinutes < nowInMinutes &&
            nowInMinutes < closeTimeInMinutes) {
          return true;
        }
    
        return false;
    
      }
    

    【讨论】:

      【解决方案2】:
       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));
        }
      

      【讨论】:

        【解决方案3】:

        如果您的时间格式是 24 小时,我有一个简单的解决方案。为此,您不需要任何外部库。

        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;
          }
        

        【讨论】:

        • 由于最后一个条件,这将不起作用。如果现在是14:15,开放时间是10:00。最后一个条件因分钟而失败
        【解决方案4】:

        您已经注意到,在我们的例子中使用DateTime 并不是最好的解决方案,因为它依赖于月/年/日。 相反,我们可以使用TimeOfDay 类,它不依赖于特定的日期,而只依赖于时间:

        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;
        }
        

        【讨论】:

        • 由于最后一个条件,这将不起作用。如果现在是14:15,开放时间是10:00。最后一个条件因分钟而失败
        【解决方案5】:

        如果您使用数字格式 [0..23] 的时间来表示小时,那么您可以将时间转换为当天过去的秒数。对您想要检查的范围执行相同操作,看看当前过去的时间(以秒为单位)是否在两个数字范围之间(以秒为单位):

        
        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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2023-04-05
          • 1970-01-01
          • 2012-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多