There's a great answer on how to get a list of incremental values for seconds for a 24-hour day. I reused a part of it.
- Note 1. I'm not sure how you're thinking of comparing
time
with datetime
. Assuming that you're just going to compare the time part and extracting that.
- Note 2. The
time.strptime
call expects a 12-hour AM/PM-based time, as in your example. Its result is then passed to time.strftime
that returns a 24-hour-based time.
Here's what I think you're looking for:
my_time = '6/23/2011 6:38:00 AM' # time you defined
from datetime import datetime, timedelta
from time import strftime, strptime
now = datetime(2013, 1, 1, 0, 0, 0)
last = datetime(2013, 1, 1, 23, 59, 59)
delta = timedelta(seconds=1)
times = []
while now <= last:
times.append(now.strftime('%H:%M:%S'))
now += delta
twenty_four_hour_based_time = strftime('%H:%M:%S', strptime(my_time, '%m/%d/%Y %I:%M:%S %p'))
twenty_four_hour_based_time in times # returns True