Home > Back-end >  Display Django time variable as full hours, minutes am/pm
Display Django time variable as full hours, minutes am/pm

Time:02-14

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 %}"   >Update</a></h7>

CodePudding user response:

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.

CodePudding user response:

You can try using built in date template tag.

{{ post.time|date:'j:i a' }}
  • Related