# Noemi Cabrera
# 19 November 2021
# In this lesson, I learned how to create timedelta objects using the timedelta() function, use
# timedelta objects to perform date arithmetic (*, /, //, -, +), compare two datetime objects
# using comparison operators, and build a useful application using timedelta arithmetic like finding
# the time until a certain date happens. Additionally, you can calculate the total number of
# seconds in a date by using the total_seconds() function. To summarize, the timedelta function
# allows you to perform arithmic operations with dates.
# I didn't have any major difficulties in this lesson.
# In this code, the timedelta function is imported from the datetime module. Then this function is used to display
# the delta time that it's specified with days, seconds, minutes, hours, and weeks. The the days, seconds, and
# microseconds attributes are accessed by writing them after the 'delta1' variable, which contains the delta time.
# These values are displayed. Then, the total seconds of the entire delta time are calculated by using the
# total_seconds() method.
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)
# In this code, the timedelta and datetime functions are imported from the datetime module. Then, datetime is used to get
# a date, which is stored in 'date1', and the current date by using today(), which is stored in 'date2'.
# With the timedelta function vailable, date1 can substracted from date2 and the result is stored in the delta2 variable.
# Lastly, the total seconds of the date are calculated by using the total_seconds() function and then displayed.
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())
# In this code, the timedelta function is imported from the datetime module. Then this function is used to display
# the delta time that it's specified with minutes, hours, and weeks.
# [ ] Create a `timedelta` object that contains: 2 hours, 3 minutes, and 1 week
from datetime import timedelta
delta_o = timedelta(minutes = 3, hours = 2, weeks = 1)
print(delta_o)
# In this code, the timedelta function is imported from the datetime module. Then this function is used to get
# the delta time that it's specified with minutes and hours. Then, the total seconds of the entire delta time
# are calculated by using the total_seconds() method. This is stored in the 't_secs' variable displayed using
# print().
# [ ] Use a `timedelta` object to calculate the total number of seconds in: 1 hour and 15 minutes
from datetime import timedelta
delta2_o = timedelta(minutes = 15, hours = 1)
t_secs = delta2_o.total_seconds()
print("Total number of seconds:",t_secs)
# In this code, the timedelta and date functions are imported from the datetime module. Then, date is used to get
# the date of my next birthday, which is stored in 'bd', and the current date, which is stored in 'cd'.
# With the timedelta function available, 'cd' is substracted from 'bd' and the result is stored in the 'd_left' variable.
# Lastly, the total days until my next birthday are displayed because '.days' is used besides the 'd_left' variable
# in the print statement.
# Use a `timedelta` object to find out how many days are left until your upcoming birthday
from datetime import timedelta, date
bd = date(month = 6, day= 24, year= 2022)
print("My next birthday is on:",bd)
cd = date.today()
d_left = bd - cd
print(d_left.days, "days are left until my next birthday")
# In this code, the timedelta and datetime functions are imported from the datetime module. Then, timedelta is used to get
# a date with only 100 days specified. The current date is get by using the datetime function with .today().
# In the 'new_date' variable, the 100 days are added to the current date. Lastly, the new date is printed in string
# format using the strftime() function.
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"))
# In this code, the timedelta and datetime functions are imported from the datetime module. Then, timedelta is used to get
# a date with only 100 days specified and this is stored in the one_hundred_days. To get 200 & 300 days, the 'one_hundred_days'
# is multiplied by 2 and 3. The current date is get by using the datetime function with .today().
# In the 'new_date1' variable, the 200 days are added to the current date. In the 'new_date2' variable, the 300 days are added
# to the current date. Lastly, the new dates are printed in string format using the strftime() function.
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"))
# In this code, the timedelta and date functions are imported from the datetime module. In the three_w variable,
# timedelta is used to get a date with only 3 weeks specified. My birthday date is get by using the date function
# and then stored in the 'bd' variable. In the 'date_three_w' variable, the 3 weeks are substracted from my
# birthday date. Lastly, the new date is printed in string format using the strftime() function.
# [ ] Write a program to compute the date 3 weeks before your birthday
# to help you remember when to send the invitations
from datetime import date, timedelta
three_w = timedelta(weeks = 3)
# Get today's date
bd = date(month= 6, day= 24, year = 2022)
print('My birthday:',bd)
# Compute the new date
date_three_w = bd - three_w
# Print formatted new date
print("Date 3 weeks before my birthday :", date_three_w.strftime("%b/%d/%Y"))
# In this code, the timedelta and date functions are imported from the datetime module. The current date is
# accessed by using the date function and .today(). Then, the current date is substracted from my birthday
# date and this is stored in the td1 variable. The id2 variable is assigned the delta time of
# 3 weeks. Lastly, in the 'td' variable, td2 is substracted from the td1 to get the date from the current date till
# the 3 weeks reminder for my birthday. The number of days from the current date till the 3 weeks reminder
# is displayed by using .days after 'td'.
# [ ] 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 date, timedelta
c_date = date.today()
td1 = bd - c_date
td2 = timedelta(weeks = 3)
td = td1 - td2
print("Number of days from the current date till the 3 weeks reminder of my birthday :", td.days)
# In this code, the date function is imported from the datetime module. This function is used to
# get two birthday dates. In the if/else statement it's determined which person is older based
# on the birthdays previously defined. The < and > operators are used to compare the dates.
# So, if birthday1 is greater than birthday2, then person with birthday1 is older. If birthday1
# is less than birthday2, then person with birthday2 is older. Else, they have the same age.
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")
# In this code, the date function is imported from the datetime module. This function is used to
# get the date for 4th of July of 2021. The current date is accessed by using date and .today().
# In the if/else statement it's if 4th of July of this year has passed or not.
# So, if current date is greater than the 4th of July date, then 4th of July has already passed.
# But if current date is not greater than the 4th of July date, , then 4th of July has already passed.
# [ ] Write a program to find out if 4th of July of this year has passed or not
from datetime import date
c_july_4th = date(year =2021, month = 7, day = 4)
c_date = date.today()
if (c_date > c_july_4th):
print("4th of July of this year has passed")
else:
print("4th of July of this year has not passed")
# In this code, the datetime function is imported from the datetime module. The current date is
# accessed by using the datetime function and '.today()'. The date for december solstice is stored
# in the 'solstice' variable. The year of the solstice is replace with the year of teh current date
# defined at the beginning. Then, the current date is substracted from the december solstice
# date and this is stored in the 'count' variable. The number of days from the current date
# till december solstice is displayed by using .days after 'count'.
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!" )
# In this code, the datetime function is imported from the datetime module. The current date is
# accessed by using the datetime function and '.today()'. The date for december solstice is stored
# in the 'december_solstice' variable. The date for july solstice is stored in the 'july_solstice'
# variable. Then, the current date is substracted from both the december solstice and july sostice
# to find out later for which solstice the time to wait is less. If the count for december solstice
# is greater than the count for july solstice, december is not closer to the current date. If it's
# not greater, then december solstice is closer to the
# current date.
# [ ] Complete the following program to find out if you are closer to the current year's June or December solstice
# Define today's date
now = datetime.today()
# Define the December solstice
december_solstice = datetime(month = 12, day = 21, year = now.year)
# Define the June solstice
june_solstice = datetime(month = 6, day = 21, year = now.year)
# Find out which solstice is closer
#TODO
count_ds = now - december_solstice
count_js = now - june_solstice
if (count_ds > count_js):
print("I am are closer to the current year's June solstice")
else:
print("I am are closer to the current year's December solstice")