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
You can use something like this
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]
Here’s some code for you:
This will output:
Hope this helps!