Dhanu P.
asked 11/30/2211.12 LAB: Dates
Complete the code to implement the following operations:
- Complete read_date():
- Read an input string representing a date in the format yyyy-mm-dd.
- Create a date object from the input string.
- Return the date object.
- Call read_date() to read four (unique) date objects and store the date objects in a list.
- Call sorted() to sort the list of date objects, earliest first. Store the sorted dates in a new list.
- Output the sorted_dates, in the format mm/dd/yy.
- Hint: Use strftime() to format the date outputs. (See resource below.)
- Output the number of days between the last two dates in the sorted list as a positive number.
- Output the date that is 3 weeks from the most recent date in the format "July 4, 1776".
- Hint: Use timedelta() to set a duration of time for the arithmetic on date objects. (See resources below.)
- Ex: timedelta(days=50, seconds=27, hours=8, weeks=2) will define a duration of 50 days + 27 seconds + 8 hours + 2 weeks.
- Output the full name of the day of the week of the earliest day.
Ex: If the input is:
the output is:
1 Expert Answer
Arwin A. answered 01/14/23
from datetime import datetime, timedelta
def read_date():
date_str = input("Enter a date in the format yyyy-mm-dd: ")
return datetime.strptime(date_str, "%Y-%m-%d")
date_list = []
for i in range(4):
date = read_date()
while date in date_list:
date = read_date()
date_list.append(date)
sorted_dates = sorted(date_list)
for date in sorted_dates:
print(date.strftime("%m/%d/%Y"))
last_date = sorted_dates[-1]
second_last_date = sorted_dates[-2]
days_diff = (last_date - second_last_date).days
print(days_diff)
three_weeks_later = last_date + timedelta(weeks=3)
print(three_weeks_later.strftime("%B %d, %Y"))
earliest_day = sorted_dates[0]
print(earliest_day.strftime("%A"))
The code above implements the prompt's operations. It uses the datetime module to create date objects, ‘strftime()’ to format the date output, ‘sorted()’ to sort the list of dates, ‘timedelta()’ to perform arithmetic on date objects and ‘strptime()’ to create date objects from the input string.
It will read four ‘(unique)’ date objects from the user and store the date objects in a list. It will sort the list of date objects, earliest first. Store the sorted dates in a new list and output the sorted_dates, in the format mm/dd/yy. This function will return a positive number indicating the number of days between the last two dates in the sorted list. This function returns the date three weeks from the most recent date using the format "July 4, 1776" with the day of the week of the earliest date output.
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Michael M.
I don't think this is the correct forum for this type of question; if you want to go through this code and talk through how to do this, I would highly suggest getting a tutor who can walk you through it. This section is more for asking specific questions - not posting an entire lab for solutions.11/30/22