Convert Year/Month/Day to Day of Year in Python
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: City Beneath the Waves Looping
--
Chapters
00:00 Convert Year/Month/Day To Day Of Year In Python
00:57 Answer 1 Score 70
01:20 Accepted Answer Score 370
01:37 Answer 3 Score 17
02:25 Answer 4 Score 9
02:54 Thank you
--
Full question
https://stackoverflow.com/questions/6203...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #datetime
#avk47
ACCEPTED ANSWER
Score 370
Use datetime.timetuple() to convert your datetime object to a time.struct_time object then get its tm_yday property:
from datetime import datetime
day_of_year = datetime.now().timetuple().tm_yday  # returns 1 for January 1st
ANSWER 2
Score 70
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.
ANSWER 3
Score 17
DZinX's answer is a great answer for the question. I found this question and used DZinX's answer while looking for the inverse function: convert dates with the julian day-of-year into the datetimes.
I found this to work:
import datetime
datetime.datetime.strptime('1936-077T13:14:15','%Y-%jT%H:%M:%S')
>>>> datetime.datetime(1936, 3, 17, 13, 14, 15)
datetime.datetime.strptime('1936-077T13:14:15','%Y-%jT%H:%M:%S').timetuple().tm_yday
>>>> 77
Or numerically:
import datetime
year,julian = [1936,77]
datetime.datetime(year, 1, 1)+datetime.timedelta(days=julian -1)
>>>> datetime.datetime(1936, 3, 17, 0, 0)
Or with fractional 1-based jdates popular in some domains:
jdate_frac = (datetime.datetime(1936, 3, 17, 13, 14, 15)-datetime.datetime(1936, 1, 1)).total_seconds()/86400+1
display(jdate_frac)
>>>> 77.5515625
year,julian = [1936,jdate_frac]
display(datetime.datetime(year, 1, 1)+datetime.timedelta(days=julian -1))
>>>> datetime.datetime(1936, 3, 17, 13, 14, 15)
I'm not sure of etiquette around here, but I thought a pointer to the inverse functionality might be useful for others like me.
ANSWER 4
Score 9
I want to present performance of different approaches, on Python 3.4, Linux x64. Excerpt from line profiler:
      Line #      Hits         Time  Per Hit   % Time  Line Contents
      ==============================================================
         (...)
         823      1508        11334      7.5     41.6          yday = int(period_end.strftime('%j'))
         824      1508         2492      1.7      9.1          yday = period_end.toordinal() - date(period_end.year, 1, 1).toordinal() + 1
         825      1508         1852      1.2      6.8          yday = (period_end - date(period_end.year, 1, 1)).days + 1
         826      1508         5078      3.4     18.6          yday = period_end.timetuple().tm_yday
         (...)
So most efficient is
yday = (period_end - date(period_end.year, 1, 1)).days + 1