Python Question: Year and Day of Year to date?
-
19-09-2019 - |
Question
I have a year value and a day of year and would like to convert to a date (day/month/year).
Thanks in advance. :)
Solution
datetime.datetime(year, 1, 1) + datetime.timedelta(days - 1)
OTHER TIPS
>>> import datetime
>>> datetime.datetime.strptime('2010 120', '%Y %j')
datetime.datetime(2010, 4, 30, 0, 0)
>>> _.strftime('%d/%m/%Y')
'30/04/2010'
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)
>>>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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow