Python format timedelta greater than 24 hours for display only containing hours?

后端 未结 3 568
春和景丽
春和景丽 2020-11-30 14:18

How do I format timedelta greater than 24 hours for display only containing hours in Python?

>>> import datetime
>>> td = datetime.timedelt         


        
3条回答
  •  佛祖请我去吃肉
    2020-11-30 14:51

    from datetime import timedelta
    from babel.dates import format_timedelta
    delta = timedelta(days=6)
    format_timedelta(delta, locale='en_US')
    u'1 week'
    

    More info: http://babel.pocoo.org/docs/dates/

    This will format your interval according to a given locale. I guess it is better, because it will always use the official format for your locale.

    Oh, and it has a granularity parameter. (I hope I could understand your question...)

提交回复
热议问题