Use datetime.strftime() on years before 1900? (“require year >= 1900”)

前端 未结 4 1905
别跟我提以往
别跟我提以往 2020-12-14 00:31

I used : utctime = datetime.datetime(1601,1,1) + datetime.timedelta(microseconds = tup[5]) last_visit_time = \"Last visit time:\"+ utctime.strftime(\'%Y-%m-%d %H:%M:%

4条回答
  •  执笔经年
    2020-12-14 01:16

    I recommend using arrow (which is an abstraction package on datetime and dateutil), it's really easy to handle every kind of datetime objects, even in Python 2.6/7.x and with dates prior to 1900.

    For example:

    >>> import arrow
    
    >>> in_date_str = "1853-10-30T13:36:41.942623+00:00"
    >>> in_date_obj = arrow.get(crea)
    >>> print(in_date_obj)
    arrow[1853-10-30T13:36:41.942623+00:00]>
    
    # basic formatting
    >>> in_date_obj.format()
    u'1853-10-30 13:36:41-00:00'
    
    # advanced formatting
    >>> in_date_obj.format("ffffdd D MMMM YYYY", "fr_FR")
    u'Dimanche 30 Octobre 1853'
    
    # humanized delta
    >>> in_date_obj.humanize()
    u'162 years ago'
    # easy localization handling
    >>> in_date_obj.humanize(locale="fr_FR")
    u'il y a 162 ans'
    

提交回复
热议问题