Extract time from datetime and determine if time (not date) falls within range?

This line:

str_time = datetime.strptime(Datetime, "%m/%j/%y %H:%M")

returns a datetime object as per the docs.

You can test this yourself by running the following command interactively in the interpreter:

>>> import datetime
>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M")
datetime.datetime(2013, 1, 31, 0, 12)

The time portion of the returned datetime can then be accessed using the .time() method.

>>> datetime.datetime.strptime('12/31/13 00:12', "%m/%j/%y %H:%M").time()
datetime.time(0, 12)

The datetime.time() result can then be used in your time comparisons.

Leave a Comment