# Noemi Cabrera
# 18 November 2021
# In this lesson, I learned how to assign and modify a time object (variable) using the the time() function,
# assign and modify a date object (variable) using the date() function, get the current local date by using
# the date or datetime functions with .today(), assign and modify a datetime object (variable) using the datetime()
# function, split a datetime object into separate time and date objects by using the indiviadula time and date
# functions on the whole date, combine time and date objects into datetime objects using the combine method, and
# display a datetime object as a formatted string using the strftime() function.
# All of these functions are imported from the datetime module.
# I didn't have any major difficulties in this lesson.
# In this code, the time function is imported from the datetime module. Then, it's used to display the time
# given the values for hour, minte, second, and microsecond in order.
from datetime import time
# Time is 8:55:20.000500 PM (or 20:55:20.000500)
t = time(20, 55, 20, 500)
print(t)
# In this code, the time function is imported from the datetime module. Then, it's used to display the time
# given the names and values values for hour, minute, second, and microsecond. However, these are not in order
# because if the names are given the function will place them in order.
from datetime import time
# Time is 9:10:20.900000 AM (or 9:10:20.900000)
t = time(minute = 10, hour = 9, microsecond = 900000, second = 20)
print(t)
# In this code, the time function is imported from the datetime module. Then, it's used to display the time
# given the the names and values for hour and minute. Since the other atributes are not defined they are set
# to 0 by default.
from datetime import time
# Time is 1:10 PM (or 13:10:00:000000)
t = time(hour = 1, minute = 10)
print(t)
# In this code, the time function is imported from the datetime module. Then, it's used to display the time
# given the the name and value for hour. Since the other atributes are not defined they are set
# to 0 by default. However, this coed displays an error because the 24 hour attribute is invalid value.
# The hour must be from 0 to 23.
from datetime import time
# Assigning a time variable with an invalid attribute
t = time(hour = 24)
print(t)
# In this code, the time function is imported from the datetime module. Then, the names and values for hour,
# minute, second, and microsecond are specified. Each of these attributes is accessed individually,
# by writing the name of the atributte after the 't', which contains the time. Each is stored in a variable.
# The final time is printed.
from datetime import time
# assign a time variable t
t = time(hour = 9, minute = 10, second = 43, microsecond = 100)
# access each of the attributes separately
h = t.hour # will be 9
m = t.minute # will be 10
s = t.second # will be 43
ms = t.microsecond # will be 100
print("The time is: ", h," hours ", m, " minutes", s, " seconds and ", ms, " microseconds" )
# In this code, the time function is imported from the datetime module. Then, the names and values for hour,
# minute, second, and microsecond are specified. The orginal time is printed. Lastly, the values for hour
# and minute are replaced with 8 and the new time is printed.
from datetime import time
# assign t as 9:10:43:0000100
t = time(hour = 9, minute = 10, second = 43, microsecond = 100)
print("Old time: ", t)
# modify hour and minute
t = t.replace(hour = 8, minute = 8)
print("New time: ", t)
# In this code, the time function is imported from the datetime module. Then, it's used to display the time
# given the the names and values for hour and minute. Since the other atributes are not defined they are set
# to 0 by default. The time is printed.
# [ ] Create a `time` variable containing the time: 8:45 AM
from datetime import time
time = time(hour= 8, minute = 45 )
print(time)
# In this code, the time function is imported from the datetime module. Then, it's used to display the time
# given the name and value for hour, minute, second, and microsecond. This is tored in the 'time' variable
# and printed.
# [ ] Create a `time` variable containing the time: 8:45:01:000150 PM
from datetime import time
time = time(hour= 8, minute = 45, second= 1, microsecond= 150 )
print(time)
# In this code, the time function is imported from the datetime module. Then, it's used to display the time
# given the name and value for hour, minute, and second. Since the other atributes are not defined they are set
# to 0 by default. This is stored in the 't3' variable. Lastly, only the value of hour is printed by
# indicating the attribute after 't3' in the print statement.
# [ ] Print the hour (only) contained in t3
from datetime import time
t3 = time(hour = 12, minute = 35, second = 2)
print( t3.hour )
# # In this code, the time function is imported from the datetime module. Then, it's used to display the time
# given the the name and value for hour and minute. Since the other attributes are not defined they are set
# to 0 by default.
# [ ] Modify t3 to: 4:10 PM
from datetime import time
t3 = time(hour = 4, minute = 10)
print( t3 )
# In this code, the date function is imported from the datetime module. Then, it's used to display 2
# dates given the values of the year, month, and day. IN the first date, the values are defined in order without the attributes
# names. In the second date, the names are specified with the values and no order is followed.
from datetime import date
# using all attributes in order (year, month, day) w/o names
# date1 is May 7 2013
date1 = date(2013, 5, 7)
print("date1 is: ", date1)
# using all attributes with names and not necessarily in order
# date2 is April 23 1999
date2 = date(day = 23, month = 4, year = 1999)
print("date2 is: ", date2)
# In this code, the date function is imported from the datetime module. Then, it's used to create the
# date given the values of the year, month, and day. This is stored inthe SpecialDate variable. Each of the
# attributes is accessed by placing a dot and the name of the attribute besides the SpecialDate variable.
# Each value is used in a print statement to display the date.
from datetime import date
# assign a date variable
SpecialDate = date(year = 2017, month = 11, day = 15)
y = SpecialDate.year # will be 2017
m = SpecialDate.month # will be 11
d = SpecialDate.day # will be 15
print("The Special Date is: / month: ", m, "/ day: ", d, "/ year: ", y)
# In this code, the date function is imported from the datetime module. Then, it's used to create a
# date given the values of the year, month, and day. This is stored in the SomeDate variable and then printed.
# The year and day of this date are modified by using the replace() function. Therefore, the year is now
# 2016, and the day is now 29. The modified date is printed.
# assign a date
SomeDate = date(year = 2015, day = 28, month = 2)
print("Old date: ", SomeDate)
# modify year and day
# 2016 is a leap year, so we can set the date to Feb 29 2016
SomeDate = SomeDate.replace(year = 2016, day = 29)
print("New date: ", SomeDate)
# In this code, the date function is imported from the datetime module. Then, it's used to display the
# date the current computer being used has. The current date is stored in the variable 'd' and displayed.
from datetime import date
# get today's date
d = date.today()
print(d)
# In this code, the date function is imported from the datetime module. Then, it's used to display the
# date given the values of the year, month, and day. This is stored in the 'date' variable and then displayed.
# [ ] Create a `date` variable containing: (March 28 2012)
from datetime import date
date = date(year= 2012, day = 28, month = 3 )
print(date)
# In this code, the date function is imported from the datetime module. Then, the user is asked to enter a month
# and day number to use to create a date. The the current year is get by using the today() function on date.
# This is stored in the c_date variable, whic is used to get the year value. All this info, is then used
# to create date specifying the attributes in order without names. The final date is displayed.
from datetime import date
# prompting the user for month/day
month_in = int(input("Enter a month number (1-12):"))
day_in = int(input("Enter a day number (1-29):"))
# get the current year
c_date = date.today()
c_year = c_date.year
# creating a new date object
f_date = date(c_year, month_in, day_in)
print(f_date)
# In this code, the datetime function is imported from the datetime module. Then, it's used to display the
# date given the values of the year, month, day, hour, and minute. In this case, this function combines the date
# and time together to displayed a much detailed date. Lastly, this function is used to display the same date
# date given the names and values for the attributes used in no specific order.
from datetime import datetime
# July 4th 2022, at 4:30 PM
# Method 1
dt = datetime(2022, 7, 4, 16, 30)
print("Method 1: ", dt)
# Method 2
dt = datetime(day = 4, month = 7, year = 2022, minute = 30, hour = 16)
print("Method 2: ", dt)
# In this code, the datetime function is imported from the datetime module. Then, it's used to display the
# date given the values of the year, month, day, hour, and minute in order. The year and minute are accessed
# individually by placing a dot and their name besides the 'dt' variable that has the date. The year and minute
# are displayed separately.
from datetime import datetime
# July 4th 2022, at 4:30 PM
dt = datetime(2022, 7, 4, 16, 30)
# access year
print("Year is: ", dt.year)
# access minute
print("Minute is: ", dt.minute)
# In this code, the datetime function is imported from the datetime module. Then, it's used to display the
# date given the values of the year, month, day, hour, and minute. The values for the year and second are
# modified using the replace() function. The modified date is printed.
from datetime import datetime
# July 4th 2022, at 4:30 PM
dt = datetime(2022, 7, 4, 16, 30)
# change year to 2020 and second to 30
dt = dt.replace(year = 2020, second = 30)
print(dt)
# In this code, the datetime function is imported from the datetime module. Then, it's used with the .today()
# function to get both the current date and time. This is stored in 'dt' and displayed.
from datetime import datetime
# get today's date and current local time
dt = datetime.today()
print(dt)
# In this code, the datetime, time, and date functions are imported from the datetime module. Then, datetime is used with the .today()
# function to get both the current date and time. This is stored in 'dt'. Then, only the time is accessed and
# displayed by using the time function after 'dt'. Lastly, only the date is accessed and displayed by using
# the date() function.
from datetime import datetime, time, date
# get today's date and current local time
dt = datetime.today()
# split into time t and date d
t = dt.time()
print("Time is: ", t)
d = dt.date()
print("Date is: ", d)
# In this code, the datetime, time, and date functions are imported from the datetime module. A time is assigned
# to the 't' variable. Then, date is used with .today() to get the current date and this is stored in 'd'.
# Lastly, these two are combined by writing them as the argument for datetime.combine(). The date and time are displayed together.
from datetime import datetime, time, date
# assign a time object
t = time(hour = 6, minute = 45, second = 0)
# assign a date object
d = date.today()
# combine t and d into a datetime object
dt = datetime.combine(date = d, time = t)
print(dt)
# In this code, the datetime function is imported from the datetime module. Then, it's used to display the
# date given the values of the year, month, day, hour, minute, and microsecond in no order because the attribute
# names are used.
# [ ] Create a `datetime` variable containing: (March 28 2012 @ 12:55:10:30 AM)
from datetime import datetime
datetime= datetime(year = 2022, month = 3, day = 28, hour= 12, minute = 55, second = 10, microsecond = 30)
print(datetime)
# In this code, the datetime function is imported from the datetime module. Then, it's used to get the
# current date and time. This is stored in the 'c_date' variable and used to get the current hour by writing
# hour next to 'c_date'. This is then stored in 'c_hou'r and displayed.
# [ ] Write code that prints the current hour
from datetime import datetime
c_date = datetime.today()
c_hour = c_date.hour
print(c_hour)
# In this code, the datetime, time, and date functions are imported from the datetime module. Then, datetime is used with the .today()
# function to get both the current date and time. This is stored in 'c_date'. Then, only the time is accessed and
# displayed by using the time function after 'c_date'. Lastly, only the date is accessed and displayed by using
# the date() function.
# [ ] Write a program that prints the current date on one line and the current time on another line
from datetime import datetime, time, date
c_date = datetime.today()
# Splitting the datetime into time/date
t = c_date.time()
print("Time is: ", t)
d = c_date.date()
print("Date is: ", d)
# In this code, the datetime, time, and date functions are imported from the datetime module. The user is asked to enter
# an hour number, whic is stored in the in_hour variable. Then, the user is asked to enter a minute number, which is
# stored in the in_minute variable. These 2 inputs are used to create a time and this is stored in 't'. Then, date is used
# with .today() to get the current date and this is stored in 'c_date'. Lastly, 't' and 'c_date'are combined by writing
# them as the argument for datetime.combine(). The date and time are displayed together.
# [ ] Write a program that:
# 1) prompts the user for a time (hours and minutes only)
# 2) gets the current date
# 3) combines the collected information into a `datetime` variable
from datetime import datetime, time, date
in_hour = int(input("Enter a hour number (1-23): "))
in_minute = int(input("Enter a minute number (1-59): "))
t = time(hour = in_hour, minute = in_minute)
c_date = date.today()
datetime= datetime.combine(date = c_date, time = t)
print(datetime)
# In this code, the time function is imported from the datetime module. Then, it's used to get the
# time given the values of the hour, and minute in no order because the attribute names are used. Then, the
# strftime() function applies a formatting directive, and returns a formatted string for 't'. Two different
# formats are applied to 't', one including AM or PM, and the other without that.
from datetime import time
t = time(hour = 10, minute = 15)
# display as 10:15 AM
# string passed to strftim includes all necessary spaces and semicolons
formatted_string = t.strftime("%I:%M %p")
print("First format: ", formatted_string)
# display as 10:15:00 (24 hour clock, no AM/PM)
formatted_string = t.strftime("%H:%M:%S")
print("Second format: ",formatted_string)
# In this code, the date function is imported from the datetime module. Then, it's used to get the
# date given the values of the year, month, and day in no order because the attribute names are used.
# This is stored in 'd' and then the strftime() function applies a formatting directive, and returns a
# formatted string for 'd'. Two different formats are applied to 'd', one including the whole month name
# and commas, and the other with the abbreviated version of the month name and no commas.
from datetime import date
d = date(year = 1999, month = 11, day =3)
# display as November, 03, 1999
# string passed to strftime includes all necessary spaces and commas
formatted_string = d.strftime("%B, %d, %Y")
print("First format: ", formatted_string)
# display as Nov 03 99
formatted_string = d.strftime("%b %d %y")
print("Second format: ", formatted_string)
# In this code, the datetime function is imported from the datetime module. Then, it's used to get the
# date & time given the values of the year, month, day , hour, and minute.
# This is stored in 'dt' and then the strftime() function applies a formatting directive, and returns a
# formatted string for 'dt'. Two different formats are applied to 'dt', one including the whole month name,
# commas plus AM/PM, and the other with the abbreviated version of the month name, no commas, and no AM/PM.
from datetime import datetime
dt = datetime(year = 1999, month = 11, day = 3, hour = 10, minute = 15)
# display as November, 03, 1999 @ 10:15 AM
formatted_string = dt.strftime("%B, %d, %Y @ %I:%M %p")
print("First format: ", formatted_string)
# display as Nov 03 99 / 10:15:00
formatted_string = dt.strftime("%b %d %y / %H:%M:%S")
print("Second format: ", formatted_string)
# In this code, the time function is imported from the datetime module. Then, it's used to get the
# time given the values of the hour, minute, and second in no order because the attribute names are used. Then, the
# strftime() function applies a formatting directive, and returns a formatted string for 't'. Two different
# formats are applied to 't', one including AM or PM, and the other without that.
# [ ] Write a program that displays the time: (17:39:10) as:
# 1) 05:39:10 PM
# 2) 17:39:10
from datetime import time
t = time(hour = 17, minute = 39, second = 10)
formatted_string = t.strftime("%I:%M:%S %p")
print("First format: ", formatted_string)
formatted_string = t.strftime("%H:%M:%S")
print("Second format: ",formatted_string)
# In this code, the date function is imported from the datetime module. Then, it's used to get the
# date given the values of the year, month, and day in no order because the attribute names are used.
# This is stored in 'd' and then the strftime() function applies a formatting directive, and returns a
# formatted string for 'd'. Four different formats are applied to 'd'.
# [ ] Write a program that displays the date: (October 23rd 2018) as:
# 1) Oct 23, 2018
# 2) 10/23/18
# 3) 23/October/2018
# 4) Tuesday October 23
from datetime import date
d = date(year = 2018, month = 10, day = 23)
# display as Nov 03 99
formatted_string = d.strftime("%b %d, %Y")
print("First format: ", formatted_string)
formatted_string = d.strftime("%m/%d/%y")
print("Second format: ", formatted_string)
formatted_string = d.strftime("%d/%B/%Y")
print("Third format: ", formatted_string)
# display as Nov 03 99
formatted_string = d.strftime("%A %B %d")
print("Fourth format: ", formatted_string)
# In this code, the date function is imported from the datetime module. Then, the weekday() function is defined.
# This function takes a date argument(variable) and returns the weekday of the date entered as the argument by
# using the strftime() function. Then I stored the date of my birthday in the 'bd' variable and entered it as
# the argument of the weekday() function. The weekday of my birthday is printed.
# [ ] Complete the function `weekday` to return the weekday name of `some_date`
# Use the function to find the weekday on which you were born
from datetime import date
def weekday(some_date):
#TODO
weekday = some_date.strftime("%A")
return weekday
# Modify to your birthdate
bd = date(day = 24, month = 6, year = 2005)
my_day = weekday(bd)
# Display the day on which you were born
print(my_day)