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 = 24)
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)
# [X] Create a `time` variable containing the time: 8:45 AM
from datetime import time
t = time(hour = 8, minute = 45)
print(t)
# [X] Create a `time` variable containing the time: 8:45:01:000150 PM
from datetime import time
t = time(hour = 20, minute = 45, second = 1, microsecond = 150)
print(t)
# [X] Print the hour (only) contained in t3
print(t.hour)
# [X] Modify t3 to: 4:10 PM
t = t.replace(hour = 16, minute = 10, second = 0, microsecond = 0)
print(t)
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)
# [X] Create a `date` variable containing: (March 28 2012)
from datetime import date
d = date(month = 3, day = 28, year = 2012)
print(d)
# [X] 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
# prompting the user for month/day
month = input("Enter month (number):")
day = input("Enter day:")
if not month.isdigit() or not day.isdigit():
print("Invalid month or date!")
else:
month = int(month)
day = int(day)
# get the current year
year = date.today().year
# creating a new date object
d = date(month = month, day = day, year = year)
print(d)
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)
# [X] Create a `datetime` variable containing: (March 28 2012 @ 12:55:10:30 AM)
from datetime import datetime
dt = datetime(2012, 3, 28, 0, 55, 10, 30)
print(dt)
# [X] Write code that prints the current hour
from datetime import datetime
print(datetime.today().time().hour)
# [X] 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()
# Splitting the datetime into time/date
d = dt.date()
t = dt.time()
print(f"Date: {d}")
print(f"Time: {t}")
# [X] 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, datetime
hour = input("Enter hour:")
minute = input("Enter minute:")
if not hour.isdigit() or not minute.isdigit():
print("Invalid hour or minute.")
else:
d = date.today()
t = time(hour = int(hour), minute = int(minute))
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)
# [X] 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)
print(t.strftime("%I:%M:%S %p"))
print(t.strftime("%H:%M:%S"))
# [X] 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(month = 10, day = 23, year = 2018)
print(d.strftime("%b %d, %Y"))
print(d.strftime("%m/%d/%y"))
print(d.strftime("%d/%B/%Y"))
print(d.strftime("%A %B %d"))
# [X] 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
return some_date.strftime("%A")
# Modify to your birthdate
wd = weekday(date(month = 1, day = 15, year = 2004))
# Display the day on which you were born
print(wd)