dos29
dos29

Reputation: 67

Display Django time variable as full hours, minutes am/pm

I want to use a time variable instead of a CharField for a model, which displays multiple saved times. My issue is it displays as "9 am" instead of "9:00 am", and "noon" instead of "12:00 pm". Can anyone help? Thanks in advance.

Relevant code below-

models.py

class Post(models.Model):
time=models.TimeField()
free=models.CharField(max_length=50, default="")
player1=models.CharField(max_length=50, default="Player 1")
player2=models.CharField(max_length=50, default="Player 2")
player3=models.CharField(max_length=50, default="Player 3")
player4=models.CharField(max_length=50, default="Player 4")

def __str__(self):
    return self.time

HTML

 <h7>{{post.time}} &nbsp; &nbsp; <a href="{% url 'update0' post.id %}"  class="btn-sm btn-success" >Update</a></h7>

Upvotes: 5

Views: 1306

Answers (2)

NixonSparrow
NixonSparrow

Reputation: 6368

You can try using built in date template tag.

{{ post.time|date:'H:i A' }}

Upvotes: 6

Lzak
Lzak

Reputation: 373

You can achieve this by just using the built-in template datetime format like so:

{{ post.time|date:"h:i A" }}

This will display the datetime as: 09:00 AM. You can read more about this in the Django docs.

Upvotes: 4

Related Questions