zeddypop
zeddypop

Reputation: 11

datetime compare time python

I have 2 events A and B. I have already created a function get_schedule(event) to get the date and time of A and B occurring in datetime. For example:

    get_schedule(a) -> datetime.datetime(2016, 1, 1, 2, 25)

How can I create a function to check if my event occurs before/after a certain timing irregardless of the date? I thought about slicing the time and comparing but I don't know how to go about it.

Upvotes: 0

Views: 4219

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121864

You can get just the time object by calling the datetime.time() method. Compare that to another time object:

# scheduled after 12 noon.
get_schedule(a).time() > datetime.time(12, 0)

Demo:

>>> import datetime
>>> datetime.datetime(2016, 1, 1, 2, 25).time()
datetime.time(2, 25)
>>> datetime.datetime(2016, 1, 1, 2, 25).time() > datetime.time(12, 0)
False

Upvotes: 1

Related Questions