Python How to Convert String to Datetime Easily
Use
datetime.strptime(your_string, format) from the datetime module to convert a string to a datetime object in Python.Examples
Input"2024-06-01"
Outputdatetime.datetime(2024, 6, 1, 0, 0)
Input"01/06/2024 14:30"
Outputdatetime.datetime(2024, 6, 1, 14, 30)
Input"June 1, 2024 2:30 PM"
Outputdatetime.datetime(2024, 6, 1, 14, 30)
How to Think About It
To convert a string to a datetime, first identify the exact format of the date and time in the string. Then use the
strptime function with a matching format string to parse it into a datetime object.Algorithm
1
Get the input string representing the date and time.2
Determine the format pattern that matches the string (e.g., year-month-day).3
Use the datetime module's strptime function with the string and format.4
Return the resulting datetime object.Code
python
from datetime import datetime # Example string date_string = "2024-06-01 14:30" # Convert string to datetime converted_date = datetime.strptime(date_string, "%Y-%m-%d %H:%M") print(converted_date)
Output
2024-06-01 14:30:00
Dry Run
Let's trace converting '2024-06-01 14:30' to datetime
1
Input string
"2024-06-01 14:30"
2
Format string
"%Y-%m-%d %H:%M"
3
Result
datetime.datetime(2024, 6, 1, 14, 30)
| Step | Value |
|---|---|
| Input string | 2024-06-01 14:30 |
| Format string | %Y-%m-%d %H:%M |
| Output datetime | 2024-06-01 14:30:00 |
Why This Works
Step 1: Import datetime module
We import datetime to access date and time functions.
Step 2: Use strptime function
strptime reads the string and converts it to a datetime object using the given format.
Step 3: Format string matches input
The format string uses codes like %Y for year and %m for month to match the input string exactly.
Alternative Approaches
Using dateutil.parser.parse
python
from dateutil.parser import parse date_string = "June 1, 2024 2:30 PM" converted_date = parse(date_string) print(converted_date)
This method automatically detects the format but requires installing the dateutil package.
Using pandas.to_datetime
python
import pandas as pd date_string = "2024-06-01 14:30" converted_date = pd.to_datetime(date_string) print(converted_date)
Pandas can parse many formats easily but adds a dependency and returns a Timestamp object.
Complexity: O(1) time, O(1) space
Time Complexity
Parsing a fixed-length string with strptime takes constant time, as it processes each character once.
Space Complexity
The function uses a fixed amount of memory to store the datetime object, so space is constant.
Which Approach is Fastest?
Using datetime.strptime is fastest and built-in, while dateutil.parser.parse is slower but more flexible.
| Approach | Time | Space | Best For |
|---|---|---|---|
| datetime.strptime | O(1) | O(1) | Known fixed formats, fastest |
| dateutil.parser.parse | O(n) | O(1) | Unknown or varied formats, flexible |
| pandas.to_datetime | O(n) | O(1) | Data analysis with pandas, many formats |
Always match the format string exactly to your date string when using
strptime.A common mistake is using the wrong format codes, causing errors or wrong dates.