Is there any way to compare two dates in the Django template language?
For example, I'd like to if the date variable due_date is before or
after the current date. The timesince and timeuntil filters work, but
they seem to require that I know ahead of time the order of the dates
I'm comparing.
Toby
There is not comparison (beyond equals / not equals) in the template
language at all. Even "ifchanged" is just an equality comparison spread
over a wide area. This is one of those "blurry dividing line" problems:
where do we stop putting the entire programming language in the
template. You just stepped on the line. :-)
> For example, I'd like to if the date variable due_date is before or
> after the current date. The timesince and timeuntil filters work, but
> they seem to require that I know ahead of time the order of the dates
> I'm comparing.
The normal approach is to add a property or a method, say past_due(), to
your object that returns a boolean based on whether the due_date is in
the past or not. Then
{% if object.past_due %}
will work intuitively.
Cheers,
Malcolm