Python Datetime Module Tutorial 🎯

beginner
22 min

Python Datetime Module Tutorial 🎯

Welcome to the Datetime Module tutorial! In this comprehensive guide, we'll explore how to work with dates, times, and durations using Python's built-in datetime module. This tutorial is designed for beginners and intermediate learners, so we'll start from the ground up and gradually delve into more advanced topics.

Understanding the Datetime Module 📝

The datetime module provides classes for manipulating dates and times, as well as functions for handling time intervals. It's a fundamental tool for many programming tasks, such as scheduling, data analysis, and web development.

python
import datetime

Creating and Manipulating Dates 📝

Creating a Date

To create a date, you can use the datetime.date class and specify the year, month, and day as arguments:

python
date_object = datetime.date(year=2022, month=12, day=31) print(date_object)

Output:

2022-12-31

Manipulating a Date

You can manipulate a date by using methods like .replace(), .year, .month, and .day:

python
date_object = datetime.date(year=2022, month=12, day=31) # Change the year new_date = date_object.replace(year=2023) print(new_date) # Change the month and day new_date = date_object.replace(month=1, day=1) print(new_date)

Output:

2023-12-31 2022-01-01

Creating and Manipulating Times 📝

Creating a Time

To create a time, you can use the datetime.time class and specify the hour, minute, and second as arguments:

python
time_object = datetime.time(hour=14, minute=30, second=0) print(time_object)

Output:

14:30:00

Manipulating a Time

You can manipulate a time by using methods like .replace(), .hour, .minute, and .second:

python
time_object = datetime.time(hour=14, minute=30, second=0) # Change the hour new_time = time_object.replace(hour=15) print(new_time) # Change the minute and second new_time = time_object.replace(minute=35, second=30) print(new_time)

Output:

15:30:00 14:35:30

Creating and Manipulating Datetime Objects 📝

Creating a Datetime Object

To create a datetime object, you can use the datetime.datetime class and specify the year, month, day, hour, minute, and second as arguments:

python
datetime_object = datetime.datetime(year=2022, month=12, day=31, hour=14, minute=30, second=0) print(datetime_object)

Output:

2022-12-31 14:30:00

Manipulating a Datetime Object

You can manipulate a datetime object by using methods like .replace(), .year, .month, .day, .hour, .minute, and .second:

python
datetime_object = datetime.datetime(year=2022, month=12, day=31, hour=14, minute=30, second=0) # Change the year new_datetime = datetime_object.replace(year=2023) print(new_datetime) # Change the month, day, hour, minute, and second new_datetime = datetime_object.replace(year=2023, month=1, day=1, hour=15, minute=35, second=30) print(new_datetime)

Output:

2023-12-31 14:30:00 2023-01-01 15:35:30

Working with Timedeltas 📝

A timedelta object represents a time duration. You can create a timedelta object using the datetime.timedelta class and specify the days, seconds, minutes, hours, and microseconds as arguments:

python
# Create a timedelta object for 1 day, 2 hours, 30 minutes, and 10 seconds timedelta_object = datetime.timedelta(days=1, hours=2, minutes=30, seconds=10) print(timedelta_object)

Output:

1 days, 2:30:10

You can add a timedelta object to a datetime object to create a new datetime object:

python
# Create a datetime object for today today = datetime.datetime.now() print(today) # Create a timedelta object for 1 day timedelta_object = datetime.timedelta(days=1) # Create a new datetime object for tomorrow tomorrow = today + timedelta_object print(tomorrow)

Output:

2022-12-31 14:30:00 2023-01-01 14:30:00

Quiz 🎯

Quick Quiz
Question 1 of 1

What is the output of `datetime.datetime.now()`?

Quick Quiz
Question 1 of 1

How do you create a datetime object for a specific date and time?