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
from datetime import datetime
day_num = "339"
print ( "The day number : " + str (day_num))
day_num.rjust( 3 + len (day_num), '0' )
year = "2020"
res = datetime.strptime(year + "-" + day_num, "%Y-%j" ).strftime( "%m-%d-%Y" )
print ( "Resolved date : " + str (res))
|
Output
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
from datetime import datetime, date, timedelta
day_num = "339"
print ( "The day number : " + str (day_num))
day_num.rjust( 3 + len (day_num), '0' )
year = "2020"
strt_date = date( int (year), 1 , 1 )
res_date = strt_date + timedelta(days = int (day_num) - 1 )
res = res_date.strftime( "%m-%d-%Y" )
print ( "Resolved date : " + str (res))
|
Output
The day number : 339
Resolved date : 12-04-2020
Time Complexity: is O(1), as it involves basic arithmetic operations, datetime operations, and string operations that take constant time.
Auxiliary Space: is O(1), as it uses only a fixed number of variables and does not require any data structure to store data.
Method #3: Using divmod()
This program converts a day number and year into a date using the datetime module in Python. It adjusts the day number and initializes a start date, and then adds the number of days to the start date using timedelta(). Finally, it formats the resulting date string using strftime() and prints the result.
Python3
from datetime import datetime, date, timedelta
day_num = "339"
print ( "The day number : " + str (day_num))
day_num = day_num.rjust( 3 + len (day_num), '0' )
year = "2020"
strt_date = date( int (year), 1 , 1 )
res_date = strt_date + timedelta(days = int (day_num) - 1 )
res = res_date.strftime( "%m-%d-%Y" )
print ( "Resolved date : " + str (res))
|
Output
The day number : 339
Resolved date : 12-04-2020
Time complexity: O(1) as it does not depend on the size of the input.
Auxiliary space: O(1) as it only uses a fixed number of variables to store the input and output, and does not depend on the size of the input.
Method 4 : date.fromordinal() method.
This method takes a proleptic Gregorian ordinal and returns the corresponding date.
Here’s the step-by-step approach:
- Initialize the day number and the year.
- Convert the year to a proleptic Gregorian ordinal using the date.toordinal() method.
- Add the day number to the ordinal obtained in step 2 and subtract 1 to get the proleptic Gregorian ordinal of the desired date.
- Convert the proleptic Gregorian ordinal to a date using the date.fromordinal() method.
- Format the date as required.
Python3
from datetime import date
day_num = "339"
print ( "The day number : " + str (day_num))
day_num = day_num.rjust( 3 + len (day_num), '0' )
year = "2020"
year_ordinal = date( int (year), 1 , 1 ).toordinal()
desired_ordinal = year_ordinal + int (day_num) - 1
res_date = date.fromordinal(desired_ordinal)
res = res_date.strftime( "%m-%d-%Y" )
print ( "Resolved date : " + str (res))
|
Output
The day number : 339
Resolved date : 12-04-2020
The time complexity of this approach is O(1) .
The Auxiliary space required is O(1) as well.
Method #5: Using dateutil.parser.parse() method
Step-by-step approach:
- Import the required module dateutil:
- Initialize the year and day number:
- Create a string representing the date using the format YYYY-DDD, where YYYY is the year and DDD is the day number:
- Use the parse() method to parse the string and convert it to a datetime object:
- Convert the datetime object to a date object:
- Format the result using the strftime() method:
- Print the result:
Python3
from datetime import timedelta
from dateutil import parser
year = "2020"
day_num = "339"
date_str = year + "-01-01"
date_obj = parser.parse(date_str)
date_res = date_obj + timedelta(days = int (day_num) - 1 )
res = date_res.strftime( "%m-%d-%Y" )
print ( "Resolved date : " + str (res))
|
OUTPUT:
Resolved date : 12-04-2020
Time complexity: The time complexity of this method is O(1) as it does not involve any loops or iterations.
Auxiliary space: The auxiliary space used by this method is O(1) as it does not require any additional data structures.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!