Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to convert a Python date string to a date object?
In this article, we convert a date string to a date object. We will use the strptime() method to convert a date string to a date object, and we will also use the dateutil module for flexible date parsing.
A date string is a date written in text form, like "09 May 2025" or "2025-05-09". To work with dates in Python, we need to convert these strings into date objects. We can achieve this using Python's datetime and dateutil modules.
Using the strptime() Function
The strptime() method takes a date string as input and converts it into a date object. The string must match the specified format pattern exactly.
Syntax
datetime.strptime(date_string, format_string)
Parameters:
date_string: The date in string format that needs to be converted
format_string: The format pattern that matches the date string structure
Example 1: Converting Date with Time and Microseconds
In this example, we convert a detailed date string containing time and microseconds ?
from datetime import datetime
# The date string with time and microseconds
time_data = "23/01/01 04:35:7.123"
# The format pattern matching the date string
format_data = "%d/%m/%y %H:%M:%S.%f"
# Convert the date string to a datetime object
date = datetime.strptime(time_data, format_data)
# Access individual components
print("Microsecond:", date.microsecond)
print("Hour:", date.hour)
print("Minute:", date.minute)
print("Second:", date.second)
print("The date is:", date)
Microsecond: 123000 Hour: 4 Minute: 35 Second: 7 The date is: 2001-01-23 04:35:07.123000
Example 2: Converting Simple Date Format
Here we convert a date string in DDMMYYYY format and extract only the date part ?
import datetime # The date string in DDMMYYYY format date_str = '09052025' # The format pattern format_str = '%d%m%Y' # Convert the date string to a datetime object datetime_obj = datetime.datetime.strptime(date_str, format_str) # Extract only the date part print(datetime_obj.date())
2025-05-09
Example 3: Converting Date with Full Month Name
This example shows how to handle date strings with full month names ?
import datetime # Date string with full month name date_str = 'May 9, 2025' # Format pattern with full month name format_str = '%B %d, %Y' # Convert to datetime object datetime_obj = datetime.datetime.strptime(date_str, format_str) # Output the date part print(datetime_obj.date())
2025-05-09
Using the dateutil Module
The dateutil module is a third-party library that provides powerful extensions to the standard datetime module. It can automatically parse dates in various formats without needing to specify the exact format string.
Example: Flexible Date Parsing
The parser.parse() function automatically detects the date format ?
from dateutil import parser # Date string in various formats date_str = 'May 10, 2025' # Parse the date string automatically datetime_obj = parser.parse(date_str) # Extract and print the date part print(datetime_obj.date())
2025-05-10
Common Date Format Codes
| Format Code | Description | Example |
|---|---|---|
| %Y | 4-digit year | 2025 |
| %y | 2-digit year | 25 |
| %m | Month as number | 05 |
| %B | Full month name | May |
| %d | Day of month | 09 |
Conclusion
Use datetime.strptime() when you know the exact format of your date string. For flexible parsing of various date formats, use the dateutil.parser.parse() method which automatically detects the format.
