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.
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.
import datetimeTo create a date, you can use the datetime.date class and specify the year, month, and day as arguments:
date_object = datetime.date(year=2022, month=12, day=31)
print(date_object)Output:
2022-12-31
You can manipulate a date by using methods like .replace(), .year, .month, and .day:
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
To create a time, you can use the datetime.time class and specify the hour, minute, and second as arguments:
time_object = datetime.time(hour=14, minute=30, second=0)
print(time_object)Output:
14:30:00
You can manipulate a time by using methods like .replace(), .hour, .minute, and .second:
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
To create a datetime object, you can use the datetime.datetime class and specify the year, month, day, hour, minute, and second as arguments:
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
You can manipulate a datetime object by using methods like .replace(), .year, .month, .day, .hour, .minute, and .second:
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
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:
# 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:
# 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
What is the output of `datetime.datetime.now()`?
How do you create a datetime object for a specific date and time?