开发者

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 microseco开发者_运维问答nd value. I am not able to locate a way to do that in the documentation, is there a simple mean to do that ?


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.


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

1000 microsecons = 1 millisecond.


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?


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' }}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜