from datetime import timedelta
# Define a timedelta object
delta1 = timedelta(days = 2, seconds = 0, minutes = 15, hours = 1, weeks = 4)
print(delta1, " is stored in delta1")
# Get the stored attributes
d = delta1.days
s = delta1.seconds
ms = delta1.microseconds
print("Days = ", d, "| Seconds = ", s, "| Microseconds = ", ms)
# Get total number of seconds
all_seconds = delta1.total_seconds()
print("Total number of seconds = ", all_seconds)
from datetime import datetime, timedelta
date1 = datetime(year = 2015, month = 1, day = 19)
date2 = datetime.today()
# Define a time delta
delta2 = date2 - date1
print(delta2, " is stored in delta2")
print("Total number of seconds = ", delta2.total_seconds())
# [ ] Create a `timedelta` object that contains: 2 hours, 3 minutes, and 1 week
from datetime import datetime, timedelta
item = timedelta(hours = 2, minutes = 3, weeks = 1 )
print(item)
# [ ] Use a `timedelta` object to calculate the total number of seconds in: 1 hour and 15 minutes
from datetime import datetime, timedelta
item = timedelta(hours = 1, minutes = 15)
print(item.total_seconds())
# Use a `timedelta` object to find out how many days are left until your upcoming birthday
from datetime import datetime, timedelta, date
from datetime import datetime, timedelta
# Define a timedelta object
one_hundred_days = timedelta(days = 100)
# Get today's date
current_date = datetime.today()
# Compute the new date
new_date = current_date + one_hundred_days
# Print formatted new date
print("After 100 days:", new_date.strftime("%b/%d/%Y"))
from datetime import datetime, timedelta
# Define the timedelta objects
one_hundred_days = timedelta(days = 100)
two_hundred_days = one_hundred_days * 2
three_hundred_days = one_hundred_days * 3
# Get today's date
current_date = datetime.today()
# Compute the new dates
new_date1 = current_date + two_hundred_days
new_date2 = current_date + three_hundred_days
# Print formatted new dates
print("After 200 days:", new_date1.strftime("%b/%d/%Y"))
print("After 300 days:", new_date2.strftime("%b/%d/%Y"))
# [ ] Write a program to compute the date 3 weeks before your birthday
# to help you remember when to send the invitations
from datetime import datetime, timedelta
birthday = datetime(year = 2022, month = 9, day = 14)
three_weeks = timedelta(weeks = 3)
send_invitations = birthday - three_weeks
print(send_invitations.strftime("%b/%d/%Y"))
# [ ] Write a program that computes the number of days from the current date till the 3 weeks reminder
# 1) Create a `timedelta` object (td1) for the period between the current date and your upcoming birthday
# 2) Create a `timedelta` object (td2) containing 3 weeks
# 3) Use the `timedelta` objects (td) from 1 and 2 to compute the required number of days
from datetime import datetime, timedelta
current_date = datetime.today()
birthday = datetime(year = 2022, month = 9, day = 14)
td1 = birthday - current_date
td2 = timedelta(weeks = 3)
send_invitations = birthday - td2
days = send_invitations - current_date
print(days)
from datetime import date
# Birthday of person 1
birthday1 = date(year = 1993, month = 3, day = 5)
# Birthday of person 2
birthday2 = date(year = 2003, month = 3, day = 20)
# Compare birthdays
if (birthday1 > birthday2):
print("Person 2 is older")
elif (birthday1 < birthday2):
print("Person 1 is older")
else:
print("Person 1 and Person 2 are of the same age")
# [ ] Write a program to find out if 4th of July of this year has passed or not
from datetime import datetime, timedelta
the_fourth = datetime(year = 2021, month = 7, day = 4)
current_date = datetime.today()
if the_fourth > current_date:
print("July 4th has not passed")
elif current_date > the_fourth:
print("July 4th has passed")
else:
print("It is July 4th")
from datetime import datetime
# Define today's date
now = datetime.today()
# December solstice of year 1, it can be any year, it will be changed later
solstice = datetime(month = 12, day = 21, year = 1)
# Change solstice's year to current year
solstice = solstice.replace(year = (datetime.today().year))
# Get the timedelta
count = solstice - now
# Display the solstice countdown
print("There are", count.days, "days until the December solstice!" )
# [ ] Complete the following program to find out if you are closer to the current year's June or December solstice
from datetime import datetime, timedelta
# Define today's date
current_date = datetime.today()
# Define the December solstice
dec_solstice = datetime(year = 2021, month = 12, day = 21)
# Define the June solstice
june_solstice = datetime(year = 2022, month = 6, day = 21)
# Find out which solstice is closer
summer_sol = june_solstice - current_date
winter_sol = dec_solstice - current_date
# Calculate number of days till each solstice
if winter_sol > summer_sol:
print("June solstice is cloer")
elif summer_sol > winter_sol:
print("December solstice is closer")
else:
print("Both solstices are equally away")
# Print the appropriate message