Skip to content
Advertisement

Python Date Comparisons

I would like to find out if a particular python datetime object is older than X hours or minutes. I am trying to do something similar to:

JavaScript

This generates a type error.

What is the proper way to do date time comparison in python? I already looked at WorkingWithTime which is close but not exactly what I want. I assume I just want the datetime object represented in seconds so that I can do a normal int comparison.

Please post lists of datetime best practices.

Advertisement

Answer

Use the datetime.timedelta class:

JavaScript

Your example could be written as:

JavaScript

or

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement