Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two dates in Django [duplicate]

Tags:

python

django

Possible Duplicate:
How to compare datetime in Django?

I need to compare 2 dates

object.submit_date.ctime() > user.last_login.ctime()

but always get false.

no matter whether last_login it is after the last submit_date

like image 802
santiago Avatar asked Oct 20 '25 20:10

santiago


2 Answers

are comparing wrong, you have to use date() or isoformat() instead of ctime()

Like this:

object.submit_date.isoformat() > user.last_login.isoformat()

this includes the time

or

object.submit_date.date() > user.last_login.date()

like image 82
Ezequiel Marquez Avatar answered Oct 22 '25 09:10

Ezequiel Marquez


If its a datetime object. you can simply compare the datetime ojects rather than using ctime.

>>> a =datetime.now()
>>> b = datetime.now()
>>> a>b
False
>>> b>a
True
like image 36
Abdul Kader Avatar answered Oct 22 '25 10:10

Abdul Kader



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!