Thursday, December 26, 2024
Google search engine
HomeLanguagesPython – Convert day number to date in particular year

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 number
day_num = "339"
 
# print day number
print("The day number : " + str(day_num))
 
# adjusting day num
day_num.rjust(3 + len(day_num), '0')
 
# Initialize year
year = "2020"
 
# converting to date
res = datetime.strptime(year + "-" + day_num, "%Y-%j").strftime("%m-%d-%Y")
 
# printing result
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




# 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 number
day_num = "339"
 
# print day number
print("The day number : " + str(day_num))
 
# adjusting day num
day_num.rjust(3 + len(day_num), '0')
 
# Initialize year
year = "2020"
 
# Initializing start date
strt_date = date(int(year), 1, 1)
 
# converting to date
res_date = strt_date + timedelta(days=int(day_num) - 1)
res = res_date.strftime("%m-%d-%Y")
 
# printing result
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




# 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 number
day_num = "339"
 
# print day number
print("The day number : " + str(day_num))
 
# adjusting day num
day_num = day_num.rjust(3 + len(day_num), '0')
 
# Initialize year
year = "2020"
 
# Initializing start date
strt_date = date(int(year), 1, 1)
 
# converting to date
res_date = strt_date + timedelta(days=int(day_num) - 1)
res = res_date.strftime("%m-%d-%Y")
 
# printing result
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:

  1. Initialize the day number and the year.
  2. Convert the year to a proleptic Gregorian ordinal using the date.toordinal() method.
  3. Add the day number to the ordinal obtained in step 2 and subtract 1 to get the proleptic Gregorian ordinal of the desired date.
  4. Convert the proleptic Gregorian ordinal to a date using the date.fromordinal() method.
  5. Format the date as required.

Python3




# Python3 code to demonstrate working of
# Convert day number to date in particular year
# Using date.fromordinal()
 
from datetime import date
 
# initializing day number
day_num = "339"
 
# print day number
print("The day number : " + str(day_num))
 
# adjusting day num
day_num = day_num.rjust(3 + len(day_num), '0')
 
# Initialize year
year = "2020"
 
# Convert year to a proleptic Gregorian ordinal
year_ordinal = date(int(year), 1, 1).toordinal()
 
# Calculate proleptic Gregorian ordinal of the desired date
desired_ordinal = year_ordinal + int(day_num) - 1
 
# Convert proleptic Gregorian ordinal to date
res_date = date.fromordinal(desired_ordinal)
 
# Format the date
res = res_date.strftime("%m-%d-%Y")
 
# printing result
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:

  1. Import the required module dateutil:
  2. Initialize the year and day number:
  3. Create a string representing the date using the format YYYY-DDD, where YYYY is the year and DDD is the day number:
  4. Use the parse() method to parse the string and convert it to a datetime object:
  5. Convert the datetime object to a date object:
  6. Format the result using the strftime() method:
  7. Print the result:

Python3




from datetime import timedelta
from dateutil import parser
 
# initializing year and day number
year = "2020"
day_num = "339"
 
# creating date string
date_str = year + "-01-01"  # January 1st of the given year
date_obj = parser.parse(date_str)  # parse date string to datetime object
 
# adding days to datetime object
date_res = date_obj + timedelta(days=int(day_num) - 1)
 
# formatting result date
res = date_res.strftime("%m-%d-%Y")
 
# printing result
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.

RELATED ARTICLES

Most Popular

Recent Comments