# 1. Brock Parker
# 2. November 9th, 2021
# 3. U3M1A1.2
# 4. Working with Dates and Times
from datetime import time
# Time is 8:55:20.000500 PM (or 20:55:20.000500)
t = time(20, 55, 20, 500)
print(t)
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)
from datetime import time
# Time is 1:10 PM (or 13:10:00:000000)
t = time(hour = 1, minute = 10)
print(t)
from datetime import time
# Assigning a time variable with an invalid attribute
t = time(hour = 29)
print(t)
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" )
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)
# [ ] Create a `time` variable containing the time: 8:45 AM
from datetime import time
t = time(hour=8, minute=45)
print(t)
# [ ] Create a `time` variable containing the time: 8:45:01:000150 PM
from datetime import time
t=time(20, 45, 1, 150)
print(t)
# [ ] Print the hour (only) contained in t3
from datetime import time
t3 = time(hour = 15, minute = 10, second = 0)
print(t3.hour)
# [ ] Modify t3 to: 4:10 PM
t3 = time(hour = 15, minute = 10, second =0)
t3=t3.replace(16,10)
print(t3)
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)
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)
from datetime import date
# 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)
from datetime import date
# get today's date
d = date.today()
print(d)
# [ ] Create a `date` variable containing: (March 28 2012)
from datetime import date
date=date(2012,3,28)
print(date)
# [ ] Prompt the user to enter a month and a day, get the current year, then create a date object with the information collected
from datetime import date
m=int(input("Please enter a numerical number for Month between 1-12: "))
d=int(input("Please enter a numerical number for day between 1-31: "))
y=int(input("Please enter a numberical number for Year: "))
print(nDate)
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)
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)
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)
from datetime import datetime
# get today's date and current local time
dt = datetime.today()
print(dt)
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)
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)
# [ ] Create a `datetime` variable containing: (March 28 2012 @ 12:55:10:30 AM)
from datetime import datetime, time, date
datetime=datetime(2021,3,28,12,55,10,30)
print(datetime)
# [ ] Write code that prints the current hour
from datetime import datetime, time
dt = datetime.today()
print("The current hour is: ", dt.hour)
# [ ] Write a program that prints the current date on one line and the current time on another line
from datetime import datetime
dt = datetime.today()
t = dt.time()
d = dt.date()
print("The current date is:",d)
print("Tge current time is:",t)
# [ ] 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 date, time, datetime
h = int(input("Enter an hour number: "))
m = int(input("Enter a minute number: "))
t = time(hour = h, minute =m)
d=date.today()
dt=datetime.combine(date=d,time=t)
print(dt)
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)
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)
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)
# [ ] Write a program that displays the time: (17:39:10) as:
# 1) 05:39:10 PM
# 2) 17:39:10
from datetime import time
ftime=time(17,39,10)
formattedTime=ftime.strftime("%I:%M:%S %p")
print("Formatted time is:",formattedTime)
print("Unformatted time is:", ftime)
# [ ] 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
dt = date(month = 10, day = 23, year =2018)
print(dt.strftime("%b %d, &Y"))
print(dt.strftime("%m/&d/&y"))
print(dt.strftime("%d/%B/%Y"))
print(dt.strftime("%A %B %d"))
# [ ] 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):
return some_date.strftime("%A")
#TODO
# Modify to your birthdate
bd = date(day = 20, month = 11, year = 2003)
# Display the day on which you were born
day = weekday(bd)
print(day)