Python – Convert day number to date in particular year
Given day number, convert to date it refers to.
Input : day_num = “339”, year = “2020”
Output : 12-04-2020
Explanation : 339th Day of 2020 is 4th December.Input : day_num = “4”, year = “2020”
Output : 01-04-2020
Explanation : 4th Day of 2020 is 4th January.
Method #1 : Using datetime.strptime()
In this, we get the year string and day number string, and pass to strptime(), converts to the corresponding required date.
Python3
# Python3 code to demonstrate working of# Convert day number to date in particular year# Using datetime.strptime()from datetime import datetime # initializing day numberday_num = "339" # print day numberprint("The day number : " + str(day_num)) # adjusting day numday_num.rjust(3 + len(day_num), '0') # Initialize yearyear = "2020" # converting to dateres = datetime.strptime(year + "-" + day_num, "%Y-%j").strftime("%m-%d-%Y") # printing resultprint("Resolved date : " + str(res)) |
The day number : 339 Resolved date : 12-04-2020
Method #2 : Using timedelta()
In this, we initialize the date by 1st of January and then add number of days using timedelta(), resultant gives the date required.
Python3
# Python3 code to demonstrate working of# Convert day number to date in particular year# Using datetime.strptime()from datetime import datetime, date, timedelta # initializing day numberday_num = "339" # print day numberprint("The day number : " + str(day_num)) # adjusting day numday_num.rjust(3 + len(day_num), '0') # Initialize yearyear = "2020" # Initializing start datestrt_date = date(int(year), 1, 1) # converting to dateres_date = strt_date + timedelta(days=int(day_num) - 1)res = res_date.strftime("%m-%d-%Y") # printing resultprint("Resolved date : " + str(res)) |
The day number : 339 Resolved date : 12-04-2020
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


