Convert Year/Month/Day to Day of Year in Python
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Riding Sky Waves v001
--
Chapters
00:00 Question
01:07 Accepted answer (Score 340)
01:28 Answer 2 (Score 60)
01:56 Answer 3 (Score 17)
02:59 Answer 4 (Score 10)
03:42 Thank you
--
Full question
https://stackoverflow.com/questions/6203...
Question links:
[datetime]: https://docs.python.org/3/library/dateti...
Accepted answer links:
[datetime.timetuple()]: https://docs.python.org/3/library/dateti...
[time.struct_time]: https://docs.python.org/3/library/time.h...
[tm_yday]: https://docs.python.org/3/library/time.h...
Answer 2 links:
[strftime]: https://docs.python.org/3/library/dateti...
[%j]: https://docs.python.org/3/library/dateti...
[DzinX's]: https://stackoverflow.com/questions/6203...
Answer 3 links:
[DZinX's answer]: https://stackoverflow.com/a/623312/40785
[DZinX's answer]: https://stackoverflow.com/a/623312/40785
--
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