Bob Reynolds
Bob Reynolds

Reputation: 1009

Show TimeField value without seconds - Django

I'm displaying a selectbox with hours in it. This hours are fixed and coming from db. For this I used models.TimeField in models.py. Now in html page these hours are shown like 14:00:00. I want to remove seconds. Here are my codes:

models.py:

class MeetingHour(models.Model):
    hour = models.TimeField()

    def __str__(self):
        return str(self.hour)

html:

<select>
{% for meetinghour in meetinghours %}
  <option value="{{meetinghour}}">{{meetinghour}}</option>
{% endfor %}
</select>

Upvotes: 2

Views: 277

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476624

You can make use of the |time template filter [Django-doc]:

<option value="{{ meetinghour }}">{{ meetinghour.hour|time:"H:i" }}</option>

Upvotes: 3

Sunny Hebbar
Sunny Hebbar

Reputation: 175

You'll need to format the relevant tag in your template using the time format, so something like this...

<option value="{{meetinghour}}">{{meetinghour|time:"H:i"}}</option>

Upvotes: 1

Related Questions