User-friendly time format in Python?

后端 未结 14 1495
[愿得一人]
[愿得一人] 2020-12-12 10:06

Python: I need to show file modification times in the \"1 day ago\", \"two hours ago\", format.

Is there something ready to do that? It should be in English.

14条回答
  •  萌比男神i
    2020-12-12 10:58

    If you happen to be using Django, then new in version 1.4 is the naturaltime template filter.

    To use it, first add 'django.contrib.humanize' to your INSTALLED_APPS setting in settings.py, and {% load humanize %} into the template you're using the filter in.

    Then, in your template, if you have a datetime variable my_date, you can print its distance from the present by using {{ my_date|naturaltime }}, which will be rendered as something like 4 minutes ago.

    Other new things in Django 1.4.

    Documentation for naturaltime and other filters in the django.contrib.humanize set.

提交回复
热议问题