skip to Main Content

I need to generate a time slot list but i don’t know how to do that.

Here is my variables;

String startTime = 08:30;
String closeTime = 22:00;
String space = 00:30;

I would like to do a list like this

[08:30, 09:00, 09:30, .... 21:30, 22:00]

So is it possible to do that ?

Thanks for helping people. This is my own solution for now:

 List<String> createTimeSlotList(int openinHour, int openingMin,
  int closingHour, int closingMin, int averageTime) {
DateTime startTime = DateTime(selectedDate.year, selectedDate.month,
    selectedDate.day, openinHour, openingMin, 0);
DateTime endTime = DateTime(selectedDate.year, selectedDate.month,
    selectedDate.day, closingHour, closingMin, 0);
Duration step = Duration(minutes: averageTime);

List<String> timeSlots = [];

while (startTime.isBefore(endTime)) {
  DateTime timeIncrement = startTime.add(step);
  timeSlots.add(DateFormat.Hm().format(timeIncrement));
  startTime = timeIncrement;
}
print(timeSlots);
return timeSlots;}

2

Answers


  1. You can use something like this

    void main() {
      String startTime = "08:30";
      String closeTime = "22:00";
      String space = "00:30";
    
      int? startTimeInMinutes = _getTimeInMinutesSinceMidnight(startTime);
      int? closeTimeInMinutes = _getTimeInMinutesSinceMidnight(closeTime);
      int? spaceInMinutes = _getTimeInMinutesSinceMidnight(space);
    
      if (startTimeInMinutes == null ||
          closeTimeInMinutes == null ||
          spaceInMinutes == null) {
        print('invalid input');
        return;
      }
    
      List<String> slots = [];
      for (int i = startTimeInMinutes;
          i <= closeTimeInMinutes;
          i += spaceInMinutes) {
        slots.add(_getTimeInStringForMinutesSinceMidnight(i));
      }
    
      print(slots);
    }
    
    int? _getTimeInMinutesSinceMidnight(String time) {
      final parts = time.split(":");
    
      if (parts.length != 2) {
        return null;
      }
    
      final a = int.tryParse(parts[0]);
      final b = int.tryParse(parts[1]);
      if (a == null || b == null) {
        return null;
      }
    
      return a * 60 + b;
    }
    
    String _getTimeInStringForMinutesSinceMidnight(int time) {
      final hours = time ~/ 60;
      final minutes = time % 60;
    
      formatTime(int val) {
        if (val < 10) {
          return "0$val";
        } else {
          return "$val";
        }
      }
    
      return "${formatTime(hours)}:${formatTime(minutes)}";
    }
    
    

    output
    [08:30, 09:00, 09:30, 10:00, 10:30, 11:00, 11:30, 12:00, 12:30, 13:00, 13:30, 14:00, 14:30, 15:00, 15:30, 16:00, 16:30, 17:00, 17:30, 18:00, 18:30, 19:00, 19:30, 20:00, 20:30, 21:00, 21:30, 22:00]

    Login or Signup to reply.
  2. Here’s some code for you:

    String startTime = "08:30";
    String closeTime = "22:00";
    String space = "00:30";
    List<String> timeSlots = [];
    Duration spaceDuration = Duration(minutes: int.parse(space.split(':')[1]), hours: int.parse(space.split(':')[0]));
    TimeOfDay start = TimeOfDay(hour: int.parse(startTime.split(':')[0]), minute: int.parse(startTime.split(':')[1]));
    TimeOfDay close = TimeOfDay(hour: int.parse(closeTime.split(':')[0]), minute: int.parse(closeTime.split(':')[1]));
    
    while (start.hour < close.hour || (start.hour == close.hour && start.minute <= close.minute)) {
      timeSlots.add(start.hour.toString().padLeft(2, "0") + ":" + start.minute.toString().padLeft(2, "0"));
      final time = DateTime(0, 0, 0, start.hour, start.minute).add(spaceDuration);
      start = TimeOfDay(hour: time.hour, minute: time.minute);
    }
    
    print(timeSlots);
    

    This will output:

    [08:30, 09:00, 09:30, 10:00, 10:30, 11:00, 11:30, 12:00, 12:30, 13:00, 13:30, 14:00, 14:30, 15:00, 15:30, 16:00, 16:30, 17:00, 17:30, 18:00, 18:30, 19:00, 19:30, 20:00, 20:30, 21:00, 21:30, 22:00]
    

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search