I have a year value and a day of year and would like to convert to a date (day/month/year).
Asked
Active
Viewed 4.5k times
6 Answers
107
datetime.datetime(year, 1, 1) + datetime.timedelta(days - 1)
Ignacio Vazquez-Abrams
- 740,318
- 145
- 1,296
- 1,325
-
3How is this simpler than SilentGhost's answer? – GreenMatt Sep 27 '12 at 16:43
-
7@GreenMatt: It doesn't involve putting the numbers into a string and then parsing that string. – Ignacio Vazquez-Abrams Sep 27 '12 at 16:46
-
2This was really meant for Mino; I guess I should have put "@Mino" in the comment, sorry. It's true that this method does not require converting numbers to a string; however, in my experience you're at least equally likely to be starting with a string. More to the point, two calls and an arithematic operation are more complicated than a single call IMO. (Granted, the number of calls will depend on any conversions needed.) – GreenMatt Sep 27 '12 at 20:18
-
I upvoted this answer because it also works for decimal days. I submitted an edit to add that. – Jeff Ellen May 11 '18 at 22:33
32
>>> import datetime
>>> datetime.datetime.strptime('2010 120', '%Y %j')
datetime.datetime(2010, 4, 30, 0, 0)
>>> _.strftime('%d/%m/%Y')
'30/04/2010'
SilentGhost
- 287,765
- 61
- 300
- 288
7
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)
sth
- 211,504
- 50
- 270
- 362
4
since it is pretty common these days, a pandas option, using pd.to_datetime with specified unit and origin:
import pandas as pd
day, year = 21, 2021
print(pd.to_datetime(day-1, unit='D', origin=str(year)))
# 2021-01-21 00:00:00
FObersteiner
- 16,957
- 5
- 24
- 56
0
>>>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)
Egor014
- 11
- 1
0
Using the mx.DateTime module to get the date is similar to what has been proposed above using datetime and timedelta. Namely:
import mx.DateTime as dt
date = dt.DateTime(yyyy,mm,dd) + dt.DateTimeDeltaFromDays(doy-1)
So, given that you know the year (say, 2020) and the doy (day of the year, say 234), then:
date = dt.DateTime(2020,1,1) + dt.DateTimeFromDays(233)
which returns
2020-08-21 00:00:00.00
The advantage of the mx.DateTime library is that has many useful features. As per description in its homepage:
- Parses date/time string values in an almost seamless way.
- Provides conversion routines to and from many different alternative date/time storage formats.
- Includes an easy-to-use C API which makes integration a breeze.
- Fast, memory efficient, accurate.
- Georgian and Julian calendar support.
- Vast range of valid dates (including B.C. dates).
- Stable, robust and portable (mxDateTime has been around for almost 15 years now).
- Fully interoperates with Python's time and datetime modules.
Use Me
- 399
- 4
- 4
-
what is the `mx` module? what does this add to a simple solution with datetime and timedelta? – FObersteiner Jan 13 '21 at 16:48
-
In this case not much. mx.DateTime has many more methods that can be used when needed. – Use Me Jan 15 '21 at 15:45
-
ok maybe you could add a link to the docs of the module, source code repo etc. – FObersteiner Jan 15 '21 at 16:01