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
t = time(hour = 8, minute = 45)
print(t)
# [ ] Create a `time` variable containing the time: 8:45:01:000150 PM
t = time(hour = 20, minute = 45, second = 1, microsecond = 150)
print(t)
# [ ] Print the hour (only) contained in t3
from datetime import time
t3 = time(hour = 15, minute = 0, second = 0)
print(t3)
# [ ] Modify t3 to: 4:10 PM
from datetime import time
t3 = time(hour = 16, minute = 10, second =0)
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)
d = date(day = 28, month = 3, year = 2012)
print(d)
# [ ] 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
d = int(input("enter a day: "))
m = int(input("enter a month number: "))
current_date = date.today()
y = current_date.year
d = date(day = d, month = m, year = y)
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)
# [ ] Create a `datetime` variable containing: (March 28 2012 @ 12:55:10:30 AM)
from datetime import datetime
d = datetime(year = 2012, month = 3, day = 28, hour = 12, minute = 55, second = 10, microsecond = 30)
print(d)
# [ ] Write code that prints the current hour
from datetime import datetime
current_day = datetime.today()
current_hour = current_day.hour
d = datetime(year = 2021, month = 3, day = 28, hour = current_hour, minute = 55, second = 10, microsecond = 30)
print(d)
# [ ] Write a program that prints the current date on one line and the current time on another line
from datetime import datetime
current_date = date.today()
print(current_date)
current_time = datetime.today()
print(current_time)
# [ ] 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
h = input("enter an hour: ")
m = input("enter a minute: ")
current_date = date.today()
datetime = 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, %A %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
t = time(hour = 17, minute = 39, second = 10)
formatted_string = t.strftime("%I,%M,%S,%p")
print(formatted_string)
new_formatted_string = t.strftime("%H,%M,%S,%p")
print(new_formatted_string)
# [ ] 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
d = date(year = 2018, month = 10, day = 23)
formatted_string = d.strftime("%b %d,%Y")
print(formatted_string)
formatted_string_two = d.strftime("%m/%d/%y")
print(formatted_string_two)
formatted_string_three = d.strftime("%d/%B/%Y")
print(formatted_string_three)
formatted_string_four = d.strftime("%A %B %d")
print(formatted_string_four)
# [ ] Complete the function `weekday` to return the weekday name of `some_date`
# Use the function to find the weekday on which you were born
def weekday(some_date):
some_date = date(year = 2006, month = 8, day = 29,)
formatted_string = some_date.strftime("%A %B %d %Y")
print(formatted_string)
weekday("Tuesday")