Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display tomorrow's date in Django template

I want to display tomorrow's date. I am using a generic ListView.

Today's date is easy via:

{% now "jS F Y H:i" %}
like image 895
Mr Panda Avatar asked Aug 26 '17 11:08

Mr Panda


2 Answers

Hope this solution will be also helpful. In your template tag register new template

import datetime
register = template.Library()

@register.filter()
def addDays(days):
   newDate = datetime.date.today() + datetime.timedelta(days=days)
   return newDate

Then in template you can use it in a different way like:

  • {{ 1|addDays }} - for tommorow
  • {{ 7|addDays }} - for next week
  • {{ 3|addDays|date:"Y-m-d" }} - for 3 days from now with date format "Y-m-d"
  • {{ -1|addDays }} - for yesterday... etc.
like image 182
Michael Stachura Avatar answered Nov 13 '22 22:11

Michael Stachura


A custom template tag would do the trick here.

from django import template
from datetime import datetime

register = template.Library()

@register.simple_tag
def tomorrow(format):
    tomorrow = datetime.date.today() + datetime.timedelta(days=1)
    return tomorrow.strftime(format)

Remember to use the format as followed by datetime module in python.

Then in template

{% tomorrow "%Y-%m-%d %H:%M" %}
like image 35
Arpit Solanki Avatar answered Nov 13 '22 22:11

Arpit Solanki