datetime Module in Python
The datetime module in Python allows you to manipulate dates and times. It provides classes for working with dates, times, durations, and time intervals.
- Introduction to the datetime Module
Before we start, let’s import the datetime module:
import datetime
Python Dates
Dates in Python are represented by the date class from the datetime module. This class is used to represent dates (year, month, day) without considering the time.
Creating Date Objects
To create a date object, you use the datetime.date class by specifying the year, month, and day.
Example 1: Creating a Date
import datetime # Creating a specific date my_date = datetime.date(2024, 8, 6) print(my_date) # Output: 2024-08-06
Example 2: Getting the Current Date
import datetime # Getting the current date current_date = datetime.date.today() print(current_date) # Output: the current date, e.g., 2024-08-06
- Date Output
Dates can be formatted and displayed in different ways using the strftime() method.
The strftime() Method
The strftime() (string format time) method allows you to format dates and times into strings according to specified formats.
Example 1: Formatting a Date
import datetime # Creating a date my_date = datetime.date(2024, 8, 6) # Formatting the date formatted = my_date.strftime("%d/%m/%Y") print(formatted) # Output: 06/08/2024
Format Codes
Here are some common format codes you can use with strftime():
- %Y: Year with century (e.g., 2024)
- %y: Year without century (e.g., 24 for 2024)
- %m: Month (01 to 12)
- %d: Day of the month (01 to 31)
- %B: Full month name (e.g., August)
- %b: Abbreviated month name (e.g., Aug)
- %A: Full weekday name (e.g., Tuesday)
- %a: Abbreviated weekday name (e.g., Tue)
Example 2: Using Format Codes
import datetime # Creating a date my_date = datetime.date(2024, 8, 6) # Formatting the date with multiple codes formatted = my_date.strftime("%A, %d %B %Y") print(formatted) # Output: Tuesday, 06 August 2024
The strptime() Method
To parse strings into date objects, you use strptime().
Example 1: Parsing a String into a Date
import datetime # String representing a date date_string = "06/08/2024" # Parsing the string into a date date_object = datetime.datetime.strptime(date_string, "%d/%m/%Y").date() print(date_object) # Output: 2024-08-06
- Manipulating Dates
Date Calculations
You can perform calculations with date objects using timedelta objects, which represent a difference between two dates.
Example 1: Adding Days
import datetime # Current date current_date = datetime.date.today() # Creating a timedelta of 10 days delta = datetime.timedelta(days=10) # Adding 10 days new_date = current_date + delta print(new_date) # Output: current date + 10 days
Example 2: Difference Between Two Dates
import datetime
# Two dates
date1 = datetime.date(2024, 8, 6)
date2 = datetime.date(2024, 8, 1)
# Calculating the difference between the two dates
difference = date1 - date2
print(difference) # Output: 5 days, 0:00:00
Conclusion
The datetime module is a powerful tool for working with dates and times in Python. You can easily create date objects, format them, and manipulate them using the features provided by this module.