63. Python's datetime Module
Python's datetime Module
The datetime module in Python provides classes for manipulating dates and times. It supports operations like formatting, parsing, arithmetic, and comparisons. Below are key features of the datetime module along with example code snippets.
1. Getting the Current Date and Time
The datetime module allows you to get the current date and time using the datetime.now() function.
import datetime
current_datetime = datetime.datetime.now()
print(current_datetime) # Output: 2025-01-22 12:00:00.123456 (example)You can also get just the current date or time:
current_date = datetime.date.today()
print(current_date) # Output: 2025-01-22
current_time = datetime.datetime.now().time()
print(current_time) # Output: 12:00:00.1234562. Creating Specific Dates and Times
You can create specific date, time, or datetime objects by passing arguments to their constructors.
from datetime import date, time, datetime
# Create a specific date
specific_date = date(2022, 5, 17)
print(specific_date) # Output: 2022-05-17
# Create a specific time
specific_time = time(14, 30, 0)
print(specific_time) # Output: 14:30:00
# Create a specific datetime
specific_datetime = datetime(2022, 5, 17, 14, 30, 0)
print(specific_datetime) # Output: 2022-05-17 14:30:003. Formatting Dates and Times
You can format datetime objects into readable strings using the strftime() method. The method takes a format string with placeholders.
Some commonly used format codes:
%Y: Year with century (e.g., 2025)%m: Month as a zero-padded decimal number (e.g., 01)%d: Day of the month (e.g., 22)%H: Hour (24-hour clock) (e.g., 12)%M: Minute (e.g., 00)%S: Second (e.g., 59)
4. Parsing Dates and Times
You can parse a string representation of a date or time into a datetime object using strptime().
5. Date Arithmetic (Adding and Subtracting Dates)
You can perform arithmetic on datetime objects using timedelta objects, which represent a difference between two dates or times.
6. Comparing Dates and Times
You can compare datetime objects using comparison operators (>, <, ==, etc.).
You can also compare time and datetime objects similarly.
7. Working with Time Zones (Using pytz)
The datetime module works with time zones through timezone objects. However, to manage time zones more effectively, you can use the pytz library.
8. Getting the Day of the Week
You can get the day of the week as an integer (0 for Monday, 6 for Sunday) using the .weekday() method, or as a string using .strftime("%A").
9. Working with Time Delays
You can pause the execution of your program for a certain period using time.sleep(), which can be useful for tasks like rate-limiting or scheduling.
10. Handling Time Zones with datetime Module
You can work with fixed offset time zones using the timezone class within the datetime module.
Summary of Common datetime Module Features:
datetime.now(): Get the current date and time.strftime(): Format adatetimeobject as a string.strptime(): Parse a string into adatetimeobject.timedelta: Perform arithmetic on dates and times.weekday(): Get the day of the week as an integer.timezone: Work with time zones and offsets.
Last updated