# My name is Pavan Kumpatla and today's date is November 30, 2021.
# I learned how to assign and modify objects (time/date/datetime) and also learned how to format
# I had no difficulties
from datetime import time
# from datetime, import time (function)
# Time is 8:55:20.000500 PM (or 20:55:20.000500)
t = time(20, 55, 20, 500)
# converts to time, stores
print(t)
# prints
from datetime import time
# from datetime, import time (function)
# Time is 9:10:20.900000 AM (or 9:10:20.900000)
t = time(minute = 10, hour = 9, microsecond = 900000, second = 20)
# converts to time, stores
print(t)
# prints
from datetime import time
# from datetime, import time
# Time is 1:10 PM (or 13:10:00:000000)
t = time(hour = 1, minute = 10)
# stores, gives data to hour and min
print(t)
# prints
from datetime import time
# from datetime, import time
# Assigning a time variable with an invalid attribute
t = time(hour = 24)
# stores, gives data to hour
print(t)
# prints
# error since it 24, it can only be below 24 and not 24 itself.
from datetime import time
# from datetime, import time
# assign a time variable t
t = time(hour = 9, minute = 10, second = 43, microsecond = 100)
# stores, gives data
# access each of the attributes separately
h = t.hour # will be 9
# stores hour
m = t.minute # will be 10
# stores min
s = t.second # will be 43
# stores second
ms = t.microsecond # will be 100
# stores microsecond
print("The time is: ", h," hours ", m, " minutes", s, " seconds and ", ms, " microseconds" )
# prints
from datetime import time
# from datetime
# assign t as 9:10:43:0000100
t = time(hour = 9, minute = 10, second = 43, microsecond = 100)
print("Old time: ", t)
# prints
# modify hour and minute
t = t.replace(hour = 8, minute = 8)
# replaces/modfies data
print("New time: ", t)
# prints
# [ ] Create a `time` variable containing the time: 8:45 AM
from datetime import time
# from datetime, import time
t = time(hour = 8, minute = 45)
# stores, gives data
print(t)
# prints
# [ ] Create a `time` variable containing the time: 8:45:01:000150 PM
from datetime import time
# from datetime, import time
t3 = time(hour = 20, minute = 45, second = 1, microsecond = 150)
# NOTE: It said "PM" -> 12 + 8 = 20
# stores, gives data
print(t3)
# prints
# [ ] Print the hour (only) contained in t3
from datetime import time
# from datetime, import time
t3 = time(hour = 3, minute = 4)
# stores, gives data
print(t3.hour)
# prints
# [ ] Modify t3 to: 4:10 PM
t3 = t3.replace(hour = 16, minute = 10)
# replaces hour and minute, stores
# NOTE:- It said "PM" -> 12 + 4 = 16
print(t3)
# prints
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
# from datetime, import date
# assign a date variable
SpecialDate = date(year = 2017, month = 11, day = 15)
# gives data, stores
y = SpecialDate.year # will be 2017
# stores year
m = SpecialDate.month # will be 11
# stores month
d = SpecialDate.day # will be 15
# stores day
print("The Special Date is: / month: ", m, "/ day: ", d, "/ year: ", y)
# prints
from datetime import date
# from datetime, import date
# assign a date
SomeDate = date(year = 2015, day = 28, month = 2)
# stores, gives data
print("Old date: ", SomeDate)
# prints
# 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)
# stores, gives data
print("New date: ", SomeDate)
# prints
from datetime import date
# from datetime, import date
# get today's date
d = date.today()
print(d)
# prints
# [ ] Create a `date` variable containing: (March 28 2012)
from datetime import date
# from datetime, import date
date1 = date(year = 2012, month = 3, day = 28)
# stores, gives data
print(date1)
# prints
# [ ] Prompt the user to enter a month and a day, get the current year, then create a date object with the information collected
# prompting the user for month/day
user1 = int(input("Type in a number for the year:- "))
user2 = int(input("Type in a number for the month between 1-12:- "))
user3 = int(input("Type in a number for the day between 1-31:- "))
# get the current year
DateToday = date(year = user1, month = user2, day = user3)
# stores, gives data (uses user input)
# creating a new date object
print(DateToday)
# prints
from datetime import datetime
# from datetime, import datetime
# July 4th 2022, at 4:30 PM
# Method 1
dt = datetime(2022, 7, 4, 16, 30)
# stores, gives data
print("Method 1: ", dt)
# prints
# Method 2
dt = datetime(day = 4, month = 7, year = 2022, minute = 30, hour = 16)
# stores, gives data
print("Method 2: ", dt)
# prints
from datetime import datetime
# from datetime, import datetime
# July 4th 2022, at 4:30 PM
dt = datetime(2022, 7, 4, 16, 30)
# stores, gives data
# access year
print("Year is: ", dt.year)
# prints
# access minute
print("Minute is: ", dt.minute)
# prints
from datetime import datetime
# from datetime, import datetime
# July 4th 2022, at 4:30 PM
dt = datetime(2022, 7, 4, 16, 30)
# stores, gives data
# change year to 2020 and second to 30
dt = dt.replace(year = 2020, second = 30)
# replaces year and second and then stores
print(dt)
# prints
from datetime import datetime
# from datetime, import datetime
# get today's date and current local time
dt = datetime.today()
# get today's date and time, stores
print(dt)
# prints
from datetime import datetime, time, date
# from datetime, import datetime, time, and date
# get today's date and current local time
dt = datetime.today()
# get today's date
# split into time t and date d
t = dt.time()
# store time
print("Time is: ", t)
# prints
d = dt.date()
# store date
print("Date is: ", d)
# prints
from datetime import datetime, time, date
# from datetime, import datetime, time, and date
# assign a time object
t = time(hour = 6, minute = 45, second = 0)
# stores, gives data
# assign a date object
d = date.today()
# stores today's date
# combine t and d into a datetime object
dt = datetime.combine(date = d, time = t)
# combines date and time and stores
print(dt)
# prints
# [ ] Create a `datetime` variable containing: (March 28 2012 @ 12:55:10:30 AM)
from datetime import datetime
# from datetime, import datetime
dt = datetime(2012, 3, 28, 12, 55, 10, 30)
# stores, gives data
print(dt)
# prints
# [ ] Write code that prints the current hour
from datetime import datetime
# from datetime, import datetime
dt = datetime.today()
# stores, gives today's date and time
print(dt.hour)
# prints hour
# [ ] Write a program that prints the current date on one line and the current time on another line
# Splitting the datetime into time/date
from datetime import datetime, time, date
# from datetime, import datetime, time, date
dt = datetime.today()
# stores today's date and time
d = dt.date()
# stores date
t = dt.time()
# stores time
print("Today's date is:-", d)
# prints
print("Today's time is:-", t)
# prints
# [ ] 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, date, time
# from datetime, import datetime, date, time
user1 = int(input("Type in the Number of Hour it is:- "))
# gathers user input, converts to int, stores
user2 = int(input("Type in the Number of Minute it is:- "))
# gathers user input, converts to int, stores
t = time(hour = user1, minute = user2)
# stores, uses user input as hour and mins
d = date.today()
# gets today's date, stores
dt = datetime.combine(date = d, time = t)
# combines date and time and stores
print(dt)
# prints
from datetime import time
# from datetime, import time
t = time(hour = 10, minute = 15)
# stores, gives data
# display as 10:15 AM
# string passed to strftim includes all necessary spaces and semicolons
formatted_string = t.strftime("%I:%M %p")
# formats t, stores
print("First format: ", formatted_string)
# prints
# display as 10:15:00 (24 hour clock, no AM/PM)
formatted_string = t.strftime("%H:%M:%S")
# formats t, stores
print("Second format: ",formatted_string)
# prints
from datetime import date
# from datetime, import date
d = date(year = 1999, month = 11, day =3)
# stores, gives data
# display as November, 03, 1999
# string passed to strftime includes all necessary spaces and commas
formatted_string = d.strftime("%B, %d, %Y")
# formats d, stores
print("First format: ", formatted_string)
# prints
# display as Nov 03 99
formatted_string = d.strftime("%b %d %y")
# formats d, stores
print("Second format: ", formatted_string)
# prints
from datetime import datetime
# from datetime, import datetime
dt = datetime(year = 1999, month = 11, day = 3, hour = 10, minute = 15)
# stores, gives data
# display as November, 03, 1999 @ 10:15 AM
formatted_string = dt.strftime("%B, %d, %Y @ %I:%M %p")
# formats dt, stores
print("First format: ", formatted_string)
# prints
# display as Nov 03 99 / 10:15:00
formatted_string = dt.strftime("%b %d %y / %H:%M:%S")
# formats dt, stores
print("Second format: ", formatted_string)
# prints
# [ ] Write a program that displays the time: (17:39:10) as:
# 1) 05:39:10 PM
# 2) 17:39:10
from datetime import time
# from datetime, import time
t = time(17, 39, 10)
# stores, gives data
ft = t.strftime("%I:%M:%S %p")
# formats t, stores
print(ft)
# prints
print(t)
# prints
# [ ] 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
# from datetime, import date
d = date(2018, 10, 23)
# stores, gives data
fd1 = d.strftime("%b %d, %Y")
# formats d, stores
print(fd1)
# prints
fd2 = d.strftime("%m/%d/%y")
# formats d, stores
print(fd2)
# prints
fd3 = d.strftime("%d/%B/%Y")
# formats d, stores
print(fd3)
# prints
fd4 = d.strftime("%A %B %d")
# formats d, stores
print(fd4)
# prints
# [ ] 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
# from datetime, import date
def weekday(some_date):
# creates new function with parameter
return some_date.strftime("%A")
# returns full day - formatted
# Modify to your birthdate
birthdate = date(2008, 5, 15)
# stores, gives data (my birthday!)
# Display the day on which you were born
print(weekday(birthdate))
# prints, calls the function