Python Date and Time Basics Quiz
A 35-question quiz covering datetime objects, current timestamps, formatting, parsing, and calculating time differences.
Question 1
Which standard library module provides classes for manipulating dates and times in Python?
Question 2
Analyze the code below. What does `dt` represent?
from datetime import datetime
dt = datetime.now()Question 3
Which class represents a duration or difference between two dates?
Question 4
Analyze the code below. What is the output format of `s`?
from datetime import date
d = date(2023, 12, 25)
s = d.strftime("%Y-%m-%d")Question 5
Which method is used to convert a string into a datetime object?
Question 6
Analyze the code below. What is the value of `future_date`?
from datetime import date, timedelta
today = date(2023, 1, 1)
future_date = today + timedelta(days=10)Question 7
What is the result of subtracting two datetime objects?
diff = datetime(2023, 1, 2) - datetime(2023, 1, 1)Question 8
Which format code represents the full name of the month (e.g., 'January')?
Question 9
Analyze the code below. What happens if the string doesn't match the format?
from datetime import datetime
dt = datetime.strptime("2023/01/01", "%Y-%m-%d")Question 10
How do you get the UNIX timestamp (seconds since epoch) from a datetime object `dt`?
Question 11
Analyze the code below. What is `t`?
from datetime import time
t = time(14, 30, 0)Question 12
Which method returns the day of the week as an integer (Monday is 0, Sunday is 6)?
Question 13
Analyze the code below. How do you create a new datetime with the year changed to 2024?
dt = datetime(2023, 5, 17)
# What goes here?Question 14
What is a 'naive' datetime object?
Question 15
Analyze the code below. What is the total duration in seconds?
from datetime import timedelta
delta = timedelta(minutes=2, seconds=30)
print(delta.total_seconds())Question 16
Which function creates a datetime object from a timestamp (e.g., 1672531200)?
Question 17
Analyze the code below. What is the output format of `isoformat()`?
dt = datetime(2023, 1, 1, 12, 0, 0)
print(dt.isoformat())Question 18
Can you add two datetime objects together (e.g., `dt1 + dt2`)?
Question 19
Which format code represents the 24-hour clock hour (00-23)?
Question 20
Analyze the code below. What does `datetime.combine()` do?
d = date(2023, 1, 1)
t = time(10, 30)
dt = datetime.combine(d, t)Question 21
What is `datetime.min`?
Question 22
Analyze the code below. What is the value of `x`?
from datetime import timedelta
x = timedelta(hours=1) == timedelta(minutes=60)Question 23
How do you access the year attribute of a datetime object `dt`?
Question 24
What happens if you try to create `date(2023, 2, 30)`?
Question 25
Which method would you use to get the current date without the time?
Question 26
Analyze the code below. What is `d.month`?
d = date(2023, 5, 17)Question 27
How does `datetime` handle leap years?
Question 28
What is the result of `datetime.now().date()`?
Question 29
Analyze the code below. What is the output?
from datetime import timedelta
t = timedelta(days=1, hours=1)
print(t.days)Question 30
Which format code represents the full 4-digit year?
Question 31
Can `timedelta` represent negative time intervals?
Question 32
Analyze the code below. What is the type of `res`?
d1 = date(2023, 1, 1)
d2 = date(2023, 1, 2)
res = d1 < d2Question 33
What is the default value for hours, minutes, and seconds if not provided to `datetime()`?
Question 34
Which method allows you to parse a string with a timezone offset like '2023-01-01T12:00:00+00:00'?
Question 35
Analyze the code below. What is the result?
from datetime import time
t = time(25, 0, 0)