DateTime compare in django template

today =  datetime.now()

{% if x.for_meeting.date_created.date < today.date and x.for_meeting.date_created.time < today.time  %}

You don't have to create the today variable (in a view), just use the {% now %} template tag already available.

And, when comparing DateTimeField objects in Django templates, make sure to compare them in the same timezones. You might want to make this explicit, for clarity and compatibility with timezone changes:

{% load tz %}

{% if date_created|utc < now|utc %}

This evaluates to False, if dead_deadline is None or => than the current datetime.

By default (for me), the date_created is in database time (UTC) and now is in localtime as set in settings.py.

Ensure also, the values you compare, have meaningful values.

PS. If you try to use the localtime filter to convert a datetime object formated to "Seconds since the Unix Epoch (January 1 1970 00:00:00 UTC)" i.e. date:"U", the output will None.


{% if date_created|date:"YmdHis" < now|date:"YmdHis" %}

{% endif %}

Tags:

Django