Python Question: Year and Day of Year to date?

╄→гoц情女王★ 提交于 2019-12-17 07:25:29

问题


I have a year value and a day of year and would like to convert to a date (day/month/year).

Thanks in advance. :)


回答1:


datetime.datetime(year, 1, 1) + datetime.timedelta(days - 1)



回答2:


>>> import datetime
>>> datetime.datetime.strptime('2010 120', '%Y %j')
datetime.datetime(2010, 4, 30, 0, 0)
>>> _.strftime('%d/%m/%Y')
'30/04/2010'



回答3:


The toordinal() and fromordinal() functions of the date class could be used:

from datetime import date
date.fromordinal(date(year, 1, 1).toordinal() + days - 1)



回答4:


>>>import datetime
>>>year = int(input())
>>>month = int(input())
>>>day = int(input())
data = datetime.datetime(year,month,day)
daynew = data.toordinal()
yearstart = datetime.datetime(year,1,1)
day_yearstart = yearstart.toordinal()
print ((daynew-day_yearstart)+1)


来源:https://stackoverflow.com/questions/2427555/python-question-year-and-day-of-year-to-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!