Friday, July 20, 2018

python - How to compare DateTime object with an integer?




How do I compare an object datetime.datetime.now().time() with an integer like 12?



I need to make an if condition, which checks whether the time of day is before 12PM or after 12PM and take corresponding action.


Answer



Simple: you don't. You create a datetime object to compare instead:



import datetime
a = datetime.datetime.now().time()
b = datetime.time(12, 00, 00, 000000)

if a < b:
print("Do x here")
else:
print("Do y here")

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...