user3854113
user3854113

Reputation:

How to check in Django if the date was yesterday

I want to compare some date fields on the db and check if they are old or they were submited today , i got this code

        {% if accounts.date.day == 13 %}
            {{ accounts.name }}
        {% endif %}

But i think this works on the day 13 of every motnh , and i dont want to only check if its day 13 i want to check if it wasnt today

I tried to do

        {% if accounts.date.day < today %}
            {{ accounts.name }}
        {% endif %}

And in the views

        today = datetime.datetime.now()

But it doesnt work either

Upvotes: 1

Views: 2990

Answers (2)

Joran Beasley
Joran Beasley

Reputation: 114098

today = datetime.date.today()  
if (today - accounts.date).days == 1:
    print "%s is yesterday!"%accounts.date

maybe??

this assumes that accounts.date is an instance of datetime.date

if {% if (today.date - accounts.date).days == 1 %} 

might work in the template ... or you could create a filter (thats what I would do

def isYesterday(a_date):
    return (datetime.date.today()-a_date).days == 1

then just do

{% if accounts.date | isYesterday %}

Upvotes: 2

slim_chebbi
slim_chebbi

Reputation: 818

if accounts.date is datetime try this

{% if accounts.date < today %}
   {{ accounts.name }}
{% endif %}

Upvotes: 3

Related Questions