How to check if a date has passed in Python (simple)

I have looked to see if I can find a simple method in Python to see if a date has passed.

For example: - If the date 01/05/2015

and date; 30/04/2015

was placed in Python, it will return True to say the date has passed.

It should be as simple and efficient as possible.

Thanks for any help.

+3


source to share


3 answers


you can use datetime, parse String to date first, then you can compare



import datetime
d1 = datetime.datetime.strptime('05/01/2015', "%d/%m/%Y").date()
d2 = datetime.datetime.strptime('30/04/2015', "%d/%m/%Y").date()
d2>d1

      

+7


source


from datetime import datetime
present = datetime.now()
print datetime(2015,04,30) < present #should return true

      



Got stuff from this question / answer: How do I compare two dates?

+3


source


Just compare them?

>>> t1 = datetime.datetime.now()
>>> t2 = datetime.datetime.now()
>>> t1>t2
False
>>> t1<t2
True

      

+2


source







All Articles