📅 Python Datetime Module
Working with dates and times is an essential part of programming. In Python, the datetime
module provides classes for manipulating dates and times easily.
Whether you need to log timestamps, format dates for reports, or calculate differences between dates, datetime
makes it possible.
In this guide, we will explore Python Dates, Date Output, Creating Date Objects, the strftime() Method, and all legal format codes.
⏰ What is Python Datetime?
The datetime
module in Python supplies classes for working with date and time.
It allows you to handle timestamps, format date output, and perform arithmetic operations on dates and times.
By default, Python doesn’t include a datatype for date/time, so this module is very important.
import datetime
📌 Python Dates
A date in Python is not a standalone data type. Instead, you use the datetime
module to work with them.
The datetime
class has attributes like year
, month
, day
, etc.
import datetime
today = datetime.datetime.now()
print(today)
Output: 2025-09-08 12:34:56.789123
📤 Python Date Output
By default, Python displays dates in the format YYYY-MM-DD HH:MM:SS
.
You can extract specific parts of the date such as the year, month, or weekday.
import datetime
x = datetime.datetime.now()
print(x.year) # 2025
print(x.strftime("%A")) # Monday
🛠️ Creating Date Objects
You can create a date object using the datetime
class. It requires year
, month
, and day
as mandatory arguments.
datetime.datetime(year, month, day)
import datetime
date1 = datetime.datetime(2025, 9, 8)
print(date1)
Output: 2025-09-08 00:00:00
📝 The strftime() Method
The strftime()
method allows you to format date objects into readable strings.
This is extremely useful when displaying dates in reports, user interfaces, or logs.
date.strftime(format)
import datetime
now = datetime.datetime.now()
print(now.strftime("%B %d, %Y"))
Output: September 08, 2025
📖 Reference of Legal Format Codes
Here are the most commonly used format codes with strftime()
:
Code | Description | Example Output |
---|---|---|
%Y | Year (full) | 2025 |
%y | Year (short) | 25 |
%m | Month (01–12) | 09 |
%B | Full month name | September |
%b | Abbreviated month name | Sep |
%d | Day of month (01–31) | 08 |
%A | Full weekday name | Monday |
%a | Abbreviated weekday | Mon |
%H | Hour (24-hour) | 14 |
%I | Hour (12-hour) | 02 |
%p | AM/PM | PM |
%M | Minutes | 45 |
%S | Seconds | 09 |
%f | Microseconds | 987654 |
%c | Local date & time | Mon Sep 08 14:45:09 2025 |
🧑💻 Exercises
- Write a program to display today’s date in “Day, Month Date, Year” format.
- Create a date object for your birthday and display the weekday name.
- Print the current time in 12-hour format with AM/PM.
- Write a program that calculates how many days are left until New Year.
❓ FAQ
Q1: How do I get only the current year?
Use datetime.datetime.now().year
.
Q2: How do I compare two dates?
You can subtract one datetime
object from another to get a timedelta
object.
Q3: What’s the difference between datetime and time modules?
datetime
handles both dates and times, while time
mostly deals with timestamps and time-related functions.
✅ Conclusion
The Python datetime module is powerful for handling real-world time-based data.
You can easily create date objects, format them with strftime()
, and use format codes to display output in human-readable ways.
By practicing with exercises, you’ll quickly get comfortable with working on date and time projects in Python.
Related Posts
- Python Programming: A Beginner’s Guide
- Python Variables
- Python Data Types
- Python Strings
- Python Operators
- Python Lists
- Python Tuples
- Python Sets
- Python Dictionaries
- Python If...Else Statements and Conditions
- Python Match Statement
- Python Functions
- Python Lambda
- Python Arrays
- Python Classes & Objects
- Python Inheritance
- Python Iterators
- Python Polymorphism
- Python Scope
- Python Modules
Continue Learning: Explore more beginner-friendly topics and practice materials in our Programming Resource Hub.
0 Comments