Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Django, how to display times with lowercase am/pm in templates?

Django's date/time formats have a code for displaying the am/pm part of times as either:

a.m.

or

AM

but not as:

am

i.e. lowercase without periods.

How do you render times with lowercase am/pm?

NOTE: I thought of the answer while typing in this question, so I figured instead of scrapping it I'd share my answer in case it's helpful to someone else.

like image 885
Ghopper21 Avatar asked Aug 31 '12 15:08

Ghopper21


People also ask

How do I create a lowercase template in Django?

Use the lower template tag with the time formatting tag, e.g. Note simply using the lower filter on a time object without the explicit time format filter doesn't work. But you can use the default time format, e.g.

How do I make the first letter capital in Django?

Use {{ obj | capfirst }} to make uppercase the first character only. Using {{ obj | title }} makes it Camel Case.

What does {% include %} do in Django?

From the documentation: {% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

How do template tags work Django?

Django Code The template tags are a way of telling Django that here comes something else than plain HTML. The template tags allows us to to do some programming on the server before sending HTML to the client.


1 Answers

Use the lower template tag with the time formatting tag, e.g.

{{object.time|time:"g:iA"|lower}}

renders as

12:30pm

compared to

{{object.time|time:"g:iA"}}

which renders as

12:30PM

Note simply using the lower filter on a time object without the explicit time format filter doesn't work. But you can use the default time format, e.g.

{{object.time|time|lower}}
like image 94
Ghopper21 Avatar answered Oct 04 '22 00:10

Ghopper21