Datetime Module
The Datetime Module in Python
Date and time manipulation is central to backend systems, financial transaction logs, scheduling engines, and data analytics. Python's built-in datetime module provides classes for managing calendar dates, times, durations, and timezones.
1. The Core datetime Classes
The module is structured into four primary classes:
date: Represents an idealized calendar date (Year, Month, Day).time: Represents an idealized time of day (Hour, Minute, Second, Microsecond), independent of any date.datetime: Combines both date and time into a single timestamp.timedelta: Represents a duration expressing the difference between two dates or times.
2. Formatting Dates with strftime()
The strftime() method (String Format Time) converts a datetime object into a custom formatted string according to format directives:
| Directive | Meaning | Example Output |
|---|---|---|
%Y | 4-digit Year | 2026 |
%m | 2-digit Month (01-12) | 09 |
%d | 2-digit Day of Month (01-31) | 12 |
%H | Hour in 24-hour clock (00-23) | 16 |
%I | Hour in 12-hour clock (01-12) | 04 |
%M | Minute (00-59) | 15 |
%S | Second (00-59) | 30 |
%p | AM / PM | PM |
%B | Full Month Name | September |
%A | Full Weekday Name | Saturday |
3. Parsing Strings into Dates with strptime()
The strptime() method (String Parse Time) converts text strings from files, databases, or APIs into datetime objects using the corresponding format string:
strftime -> Format to string.
- strptime -> Parse from string.4. Date Arithmetic with timedelta
timedelta allows calculating deadlines, age, past dates, and future intervals effortlessly:
5. Naive vs. Aware Datetime Objects
- Naive: Contains no timezone information. Python cannot determine what geographical timezone it belongs to.
- Aware: Holds timezone metadata (such as UTC, IST, or EST) via the
tzinfoparameter, preventing ambiguity across distributed servers.
Multiple Choice Questions
1. Which method converts a datetime object into a custom formatted string?
A. datetime.strptime() B. datetime.to_string() C. datetime.strftime() D. datetime.parse() Answer: C Explanation: strftime() ("String Format Time") formats a datetime object into a string representation according to specified format directives.
2. What does strptime("25/12/2026", "%d/%m/%Y") produce?
A. A string "25-12-2026" B. A datetime object representing December 25, 2026 C. A timestamp integer D. A syntax error Answer: B Explanation: strptime() parses a string into a Python datetime object according to the directive tokens provided.
3. Which class represents the elapsed duration or difference between two datetime instances?
A. datetime.duration B. datetime.timedelta C. datetime.timespan D. datetime.interval Answer: B Explanation: timedelta represents differences between two dates or times, supporting arithmetic operations like addition and subtraction.
4. What is the difference between a "naive" and an "aware" datetime object in Python?
A. Naive objects have microsecond precision, while aware objects do not B. Aware objects contain explicit timezone offset information (tzinfo), whereas naive objects do not C. Naive objects are deprecated in Python 3 D. Aware objects can only be created on Linux Answer: B Explanation: An aware datetime has an associated tzinfo indicating its geographical timezone offset; naive datetimes lack timezone context.
5. What directive in strftime() represents the 4-digit year?
A. %y B. %Y C. %YEAR D. %d Answer: B Explanation: %Y outputs the 4-digit year (e.g. 2026), whereas lowercase %y outputs the 2-digit year (e.g. 26).
Collections Module
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Math and Random Module | Collections Module |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.