Convert Year/Month/Day to Day of Year in Python

前端 未结 6 1653
不知归路
不知归路 2020-11-29 17:32

I\'m using the datetime module, i.e.:

>>> import datetime
>>> today = datetime.datetime.now()
>>> print(today)
2009-03-06 13:24:58.         


        
6条回答
  •  自闭症患者
    2020-11-29 17:43

    You could use strftime with a %j format string:

    >>> import datetime
    >>> today = datetime.datetime.now()
    >>> today.strftime('%j')
    '065'
    

    but if you wish to do comparisons or calculations with this number, you would have to convert it to int() because strftime() returns a string. If that is the case, you are better off using DzinX's answer.

提交回复
热议问题