Whirly
Whirly

Reputation: 294

How to display milliseconds instead of microseconds with Django

In Django template language I can display a time with microseconds using :

{{ player.time_to_display|time:"i:s:u" }}

I would like to display only milliseconds instead of the full microsecond value. I am not able to locate a way to do that in the documentation, is there a simple mean to do that ?

Upvotes: 1

Views: 3242

Answers (4)

Mike Fogel
Mike Fogel

Reputation: 3197

I wouldn't call this simple, but I think this will give you what you're looking for.

{{ player.time_to_display|time:'i:s' }}:{{ player.time_to_display|time:'u'|add:'0'|stringformat:'06i'|slice:':3' }}

Upvotes: 0

dmitko
dmitko

Reputation: 2657

Try the following:

{{ player.time_to_display|time:"i:s" }}:{{ player.time_to_display|time:"u"|slice:":3" }}

This separates milliseconds and takes just 3 first letters. However it does not do rounding - but do you really need it at such tiny values?

Upvotes: 0

Manoj Govindan
Manoj Govindan

Reputation: 74705

There is no built in support for displaying milliseconds in Django, or in Python for that matter. Your best bet would be to implement a custom filter that accepts a datetime instance and do the conversion yourself.

Upvotes: 2

deRailed
deRailed

Reputation: 579

What about dividing your microseconds by 1000 (and maybe round the new value off)?

1000 microsecons = 1 millisecond.

Upvotes: 1

Related Questions