How do I format datetime to give me only the time in my model
+7
A:
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")
Dominic Rodger
2009-10-13 08:37:56