-1

Using python, I want to get all the times within a range (24 hour time). How would I do this?

If

start="10:00"
end="10:05"

Then I would want to get

["10:00","10:01","10:02","10:03","10:04","10:05"]
Alexander
  • 1,051
  • 1
  • 8
  • 21

1 Answers1

1

Using the date time module might be useful. Here's my idea for your problem if you were to use military time:

import datetime

start = datetime.time(10,0) # 10:00
end = datetime.time(10,5) # 10:05
TIME_FORMAT = "%H:%M" # Format for hours and minutes
times = [] # List of times 
while start <= end:
    times.append(start)
    if start.minute == 59: # Changes the hour at the top of the hour and set the minutes back to 0
        start = start.replace(minute=0) # have to use the replace method for changing the object
        start = start.replace(hour=start.hour + 1)
    else:
        start = start.replace(minute=start.minute + 1)
times = [x.strftime(TIME_FORMAT) for x in times] # Uses list comprehension to format the objects
print(times)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Asif Ally
  • 41
  • 4