How to get only time in models

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

+2


source to share


1 answer


If you have an object datetime

in your template foo

use:

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

      

Take a look at the time

filter documentation . You are not limited "H:i"

, there are many options for formatting objects datetime

.



If for some reason you want to do this right in your model (which is probably not where it is), use datetime

strftime

- look at the appropriate one.

For example, again assuming an object datetime

named foo

:

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

      

+7


source







All Articles