W3Schools Python DateTime Module: Mastering Dates and Times efficiently

Logan Jul 13, 2026

Welcome to our comprehensive guide on leveraging the datetime module in Python, facilitated by none other than the acclaimed learn-by-doing platform, W3Schools. If you're eager to manipulate dates and times in your Python projects or automate date-based tasks, you're in the right place.

Python calendar Module with Example
Python calendar Module with Example

The Python datetime module offers a plethora of functionalities to handle dates and time, enabling you to extract, format, and perform calculations with dates. Let's dive into the world of datetime with W3Schools, your trusted companion on this coding journey.

календар в python
календар в python

Datetime Objects

At the core of datetime module lie datetime objects, which encapsulate date and time information. These objects are Instantiateable using the datetime() function, as shown:

Calendar With Events In Python Flask
Calendar With Events In Python Flask

from datetime import datetime now_d = datetime.now()

Basic datetime Constructor

python aesthetic
python aesthetic

The simplest way of creating a datetime object is by using the .now() method, as indicated above. Here's what you'll get:

print(now_d) # Output: 2022-07-14 12:00:00.000000

Custom datetime Constructor

Python Program to output Calendar for a month - EasyCodeBook.com
Python Program to output Calendar for a month - EasyCodeBook.com

If you wish to work with a specific date and time, you can manually set it, like so:

custom_d = datetime(2022, 7, 14, 12, 30, 45) print(custom_d) # Output: 2022-07-14 12:30:45

Formatting Dates

Learn Python in 10 days
Learn Python in 10 days

Python's strftime() function is your friend when it comes to formatting dates and times per your requirements. W3Schools' interactive examples make learning strftime a breeze.

strftime() in Action

DIGITAL CLOCK | PYTHON -
DIGITAL CLOCK | PYTHON -
Python to_timestamp Easiest Way to Implement with Examples
Python to_timestamp Easiest Way to Implement with Examples
Pinball Game development using Python
Pinball Game development using Python
Python Coding Interview Question: Test Your Python Deque Skills! 🐍
Python Coding Interview Question: Test Your Python Deque Skills! 🐍
an iphone screen showing the web page for python central, which is open to several different languages
an iphone screen showing the web page for python central, which is open to several different languages
Python variables cheat sheet
Python variables cheat sheet
Top Python Modules for Data Visualization, GUI Development, Web Development, Web Scraping, etc
Top Python Modules for Data Visualization, GUI Development, Web Development, Web Scraping, etc
30 Python Projects For Beginners and Advanced Learners (2023)
30 Python Projects For Beginners and Advanced Learners (2023)
an advertisement for the company's website, which is designed to look like it has been
an advertisement for the company's website, which is designed to look like it has been
30-Day Python Learning Roadmap for Beginners | Step-by-Step Coding Plan 2026
30-Day Python Learning Roadmap for Beginners | Step-by-Step Coding Plan 2026
what python can do diagram with icons and symbols
what python can do diagram with icons and symbols
the four basic python data structures are shown in this screenshoter's guide
the four basic python data structures are shown in this screenshoter's guide
Python Roadmap 2026 for Beginners | Learn Python Step by Step
Python Roadmap 2026 for Beginners | Learn Python Step by Step
Day 27: NumPy in Python for Beginners 🧮
Day 27: NumPy in Python for Beginners 🧮
Python Adventure Awaits
Python Adventure Awaits
| Pythonista Planet
| Pythonista Planet
the text reads, 200 + machine learning projects for beginners
the text reads, 200 + machine learning projects for beginners
Python Projects Ideas For Beginners
Python Projects Ideas For Beginners
a diagram showing data structures in python
a diagram showing data structures in python
Python dictionary view objects cheat sheet
Python dictionary view objects cheat sheet

Use strftime() to format the datetime object 'now_d' like you desire:

formatted_now = now_d.strftime('%Y-%m-%d %H:%M:%S') print(formatted_now) # Output: 2022-07-14 12:00:00

Pick Your Format

Explore different format directives in the strftime() function, such as %Y for year, %m for month, and %d for day. Mix and match them to return your desired date format.

Calculations with datetime

You can also perform arithmetic operations with date and time objects. Say you wish to find out what time it will be after a certain period.

Adding to datetime

Adding seconds, minutes, hours, or even days to a datetime object is straightforward:

from datetime import timedelta two_hours_later = now_d + timedelta(hours = 2) print(two_hours_later) # Output: 2022-07-14 14:00:00

Subtracting from datetime

Similarly, find out past times just as effortlessly:

two_hours_ago = now_d - timedelta(hours = 2) print(two_hours_ago) # Output: 2022-07-14 10:00:00

With W3Schools' robust guidance on Python's datetime functions and interactive examples, you're ready to concoct formidable date and time-based Python applications. Happy coding!