Reputation: 179
final response = await apiResponseMaker(result, ModelType.working_hour);
if (response['success']) {
List<WorkingHourModel> workingHours = response['hours'];
int startTime = int.parse(workingHours[0].startTime.split(':')[0]);
int endTime = int.parse(workingHours[0].endTime.split(':')[0]);
int hoursLeft = endTime - startTime + 1;
print('Hours-------- $hoursLeft');
List<String> hours = List.generate(hoursLeft * 2, (i) => '${endTime(i/2).truncate()}:${i%2 == 1 ? '00' : '30'}').reversed.toList();
hours.forEach(print);
print('Hours-------- $hours');
return {
'success': true,
'hours': hours,
};
}
return response;
}
the response i get is:
{"success":true,"working_hours":[{"day":"Monday","start_time":"09:00","end_time":"17:00","is_holiday":false}]}
This gives list of hours
[9:00, 9: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]
The end hour is 17:00. but its showing extra adding 17:30. This happens every time when an hour is ending with __:00.
For example: If response is
{"day":"Monday","start_time":"11:00","end_time":"15:00","is_holiday":false}
the hours will be:
[11:00, 11:30, 12:00, 12:30, 13:00, 13:30, 14:00, 14:30, 15:00, 15:30]
Upvotes: 0
Views: 1376
Reputation: 5
you can use this code
List<String> generateTimeIntervals(String startTime, String endTime) {
final start = DateTime.parse("2022-02-22 $startTime");
final end = DateTime.parse("2022-02-22 $endTime");
final result = <String>[];
while (start.isBefore(end)) {
result.add(formatTime(start));
start = start.add(Duration(minutes: 30));
}
return result;
}
String formatTime(DateTime time) {
return "${time.hour.toString().padLeft(2, '0')}:${time.minute.toString().padLeft(2, '0')}";
}
Upvotes: 0
Reputation: 4781
You can use TimeOfDay class from Flutter and Duration API to achieve the expected result. Here is the sample snippet.
Logic of splitting slots
Iterable<TimeOfDay> getTimeSlots(TimeOfDay startTime, TimeOfDay endTime, Duration interval) sync* {
var hour = startTime.hour;
var minute = startTime.minute;
do {
yield TimeOfDay(hour: hour, minute: minute);
minute += interval.inMinutes;
while (minute >= 60) {
minute -= 60;
hour++;
}
} while (hour < endTime.hour || (hour == endTime.hour && minute <= endTime.minute));
}
Use them like below
final startTime = TimeOfDay(hour: 9, minute: 0);
final endTime = TimeOfDay(hour: 22, minute: 0);
final interval = Duration(minutes: 30);
final times = getTimeSlots(startTime, endTime, interval)
.toList();
print(times);
End result will be the array of slots in TimeOfDay
format. You can format it either in the getTimeSlots
method or after receiving the array.
Upvotes: 0
Reputation: 577
Looks like your logic to generate the list is not correct. You can try the following:
List<String> hours = List.generate(hoursLeft * 2 - 1, (i) => '${startTime + (i/2).floor()}:${i%2 == 0 ? '00' : '30'}').toList();
First you need to generate N records where N = hoursLeft * 2 - 1
. The minus one is because otherwise you will generate one more record which is 17:30 because 18 - 9 = 9
and you will generate 9 * 2 = 18
records but you need only 17 to reach 17:00. Also in your code you use endTime(i/2)
which should throw an error since endTime is not a method and can't be executed that way.
Upvotes: 1