dhooonk
dhooonk

Reputation: 2625

How can I subtract like end_date - start_date in django template

I'd like to subtract date in template but don't know how.

is there any way to subtract two column in django template?

template.html

                {% for career in careers %}
                    {% if career.user == user%}
                    <div class="flex">
                        <div class="w-1/4">
                            <p class="font-thin">{{career.job}}</p>
                        </div>
                        <div class="w-1/4">
                            <p class="font-thin">{{career.company}}</p>
                        </div>
                        <div class="w-1/4">
                            <p class="font-thin">{{career.position}}</p>
                        </div>
                        <div class="w-1/4">
                            <p class="font-thin">{{career.end_date}}-{{career.start_date}}</p> //as clearly, it doesn't work.
                        </div>
                    </div>  
                    {% endif %}
                {% endfor %}

Upvotes: 0

Views: 133

Answers (1)

Pruthvi Barot
Pruthvi Barot

Reputation: 2018

you can do this with @property function in models.py

in models.py

class <YourClassName>(models.Model):
    ...
    @property
    def get_duration(self):
        dt = self.end_date - self.start_date
        # with dt you can get days and seconds and you can covert or format in week or hours whatever you want
        return dt.days # returns number of days

then in template simply use

{{career.get_duration}}

Upvotes: 2

Related Questions