user167139
user167139

Reputation: 279

how do I get only the time in models

How do I format datetime to give me only the time in my model

Upvotes: 2

Views: 100

Answers (1)

Dominic Rodger
Dominic Rodger

Reputation: 99751

If you've got a datetime object in your template called foo, use:

{{ foo|time:"H:i" }}

Look at the time filter documentation. You're not limited to "H:i", there are lots of options around for formatting datetime objects.

If for some reason you're wanting to do this directly in your model (which is probably not where it belongs), then use datetime's strftime - take a look at the relevant documentation.

For example, again assuming a datetime object called foo:

hour = foo.strftime("%H:%M")

Upvotes: 7

Related Questions