-1

If i have a list from 1 to 365, how could i find the date given an n day from the list?

For example if i sample from the list the number 10, i should get 10/01/2020, if i sample the number 32, i should get 01/02/2020.

I have seen that there is a way to do the inverse operation. (Get the day of the year given a date) here but i couldn't find what i'm looking for exactly.

Let's consider no leap in years.

Is there any way to perform such an operation in Python 3?

Thank you very much in advance

Miguel 2488
  • 1,410
  • 1
  • 20
  • 41
  • 1
    How are you going to handle leap years? The 29th day can be either 1.3 or 29.2, which will then offset any later day by 1 – DeepSpace Oct 15 '20 at 16:05
  • let's consider just the basic case. 29th day will be 1.3 – Miguel 2488 Oct 15 '20 at 16:06
  • Research the `calendar` module, it can tell you how many days each month has (**while** dealing correctly with leap years). Then it is just a matter of simple math – DeepSpace Oct 15 '20 at 16:08
  • Does this answer your question? [How to convert Julian date to standard date?](https://stackoverflow.com/questions/37743940/how-to-convert-julian-date-to-standard-date) – Tyler Oct 15 '20 at 16:23
  • Is not really what i needed but it's ok. Thanks :) – Miguel 2488 Oct 15 '20 at 16:25

2 Answers2

1
datetime.strptime("2020-032", "%Y-%j")

gives

datetime.datetime(2020, 2, 1, 0, 0)
Lukas S
  • 3,212
  • 2
  • 13
  • 25
1

I hope, this is the code you actually want. As you want a list of days, so I used day_list here though it's redundant.

Code

from datetime import date, timedelta

fixed_date = date(2020, 1, 10) 
  
print("A fixed date : {}".format(fixed_date))
day_list = [a for a in range(1,366) ]
for i in [10, 32]:
    day_shift = int(day_list[i-1])
    new_date = fixed_date + timedelta(days = day_shift)
    print("New date after {} days: {}".format(i, new_date))

Output

A fixed date : 2020-01-10
New date after 10 days: 2020-01-20
New date after 32 days: 2020-02-11
KittoMi
  • 411
  • 5
  • 19