Search code examples
djangodjango-templatesdjango-template-filters

Implementing countdown in django in template


I have a bootstrap template where the countdown is already implemented. And the time is hardcoded like below.

<div  data-countdown="2020/03/01"></div>

here, if I change the number like 2020 it shows the calculated time. But I didn't want this hardcoded time or number. I want to put my actual time from my DateTime fields. So how can I use my DateTime variable as a template variable and put it as the parameter of the data-countdown attribute above? Like we do with {{}} .


Solution

  • Use the django template filter like this

    <div data-countdown="{{ datetime_from_context|date:"Y/m/d" }}"></div>
    

    Use this as a reference incase if you want to change the format: https://simpleisbetterthancomplex.com/references/2016/06/21/date-filter.html