John Smith
John Smith

Reputation: 943

Formating Django TimeField in a Django view

How do you format a TimeField in a Django view?

Currently in my django html template I can easily do something like: {{movie.start_time|time:"g:iA"|lower}}

How can I do the equivalent of the above in a Django view?

Upvotes: 1

Views: 1385

Answers (2)

John Smith
John Smith

Reputation: 943

Below is an example of how to achieve this. It isn't ideal, but this is what I came up with.

import datetime

now = datetime.datetime.now()
time = now.time()

t = time.strftime("%I:%M %p")
t = t.lower()
t = list(t)

if t[0] == '0':
    t.pop(0)

t = ''.join(t)
print t

Upvotes: 0

John Gordon
John Gordon

Reputation: 33275

Use the Python strftime() function. https://docs.python.org/2/library/time.html#time.strftime

Upvotes: 2

Related Questions