1

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

2 Answers2

3

if accounts.date is datetime try this

{% if accounts.date < today %}
   {{ accounts.name }}
{% endif %}
slim_chebbi
  • 798
  • 6
  • 9
  • That doesnt returns anything , in all cases –  Aug 13 '14 at 21:33
  • and accounts.date.day < today.day – slim_chebbi Aug 13 '14 at 21:41
  • Yes , but i have the same problem as before , i dont know if maybe i am at day 5th and the account date is 5th of the month before , will it count as true ? cause they have the same day –  Aug 13 '14 at 21:48
  • with this code will count as false this is why you must compare 2 datetime object,see this answers may help you http://stackoverflow.com/questions/3798812/how-to-compare-dates-in-django – slim_chebbi Aug 13 '14 at 22:03
  • @Maks Yes, `date(2015, 6, 20) < date(2015, 7, 20)` is `True`. – Quentin Pradet Jun 24 '15 at 07:17
2
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 %}
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • Is there a way to do that in the template ? or do i have to do it on the urls.py or other .py file by force ? –  Aug 13 '14 at 21:34
  • No, you either need to create a templatetag like the isYesterday shown above or do this on the Account model or do it in the view logic – Apollo Data Aug 07 '23 at 18:57