user2106353
user2106353

Reputation: 1249

Add one day in time from database in django

Hi,

I need to add some condition on the time base , right now the value of time i am getting from database is like 2012-09-05 05:05:05 and i need to add 24 hours in this.

I need to show different content on time base, like for original time i need to show "hi" and after 24 hours i need to show "hello" in the template file.

How can i do this? Please help me.

Upvotes: 1

Views: 2138

Answers (3)

Jack Shedd
Jack Shedd

Reputation: 3531

You could also do this as a cheap filter.

from django import template
register = template.Library()
@register.filter()
def is_at_least_24_hours_ago(value):
    import datetime
    original_date_plus_24 = value + datetime.timedelta(hours=24)
    right_now = datetime.datetime.now()
    if right_now >= original_date_plus_24:
        return True
    return False

Then in your template:

{% if object_date|is_at_least_24_hours_ago %}
    // It's 24 hours later
{% else %}
    // It's not
{% endif %}

Upvotes: 2

yuwang
yuwang

Reputation: 876

probably a custom template tag would be a nice way: https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters . you could implement your own deltaDays tag with parameters, i.e. {{ date|deltaDays:1 }}.

Upvotes: 2

catherine
catherine

Reputation: 22808

This is sample on how to add day(s),

import datetime
b = var_name + datetime.timedelta(0,3) # days, seconds

or 

datetime.timedelta(days=1)

pass the value through view then use if statement in your template

Upvotes: 1

Related Questions