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
Convert \"unknown format\" strings to datetime objects in Python
Dates can be in many formats like "2009/05/13 19:19:30", "May 13 2009 07:19PM", and "2009-05-13 19:19". Python provides several approaches to convert these unknown format strings into datetime objects using the datetime and dateutil modules.
A Python datetime object contains complete information about date and time including year, month, day, hours, minutes, seconds, and time zones. This article shows how to convert unknown format date strings to datetime objects.
Input-Output Example
Here's what we want to achieve ?
Input string (unknown format): 20050607T090650 Output Datetime object: 2005-06-07 09:06:50 Data type: <class 'datetime.datetime'>
Using datetime.strptime() Method
The strptime() method parses a string into a datetime object using specific format codes.
Syntax
datetime.strptime(date_string, format_string)
Common Format Codes
| Code | Description | Example |
|---|---|---|
| %Y | Year with century | 2023 |
| %m | Month as zero-padded number | 03 |
| %d | Day as zero-padded number | 15 |
| %H | Hour (24-hour) | 14 |
| %M | Minute | 30 |
| %S | Second | 45 |
| %z | UTC offset | +0530 |
Example 1: ISO Format with Timezone
from datetime import datetime
date_string = '20151002151700-0800'
print("Input string:", date_string)
obj = datetime.strptime(date_string, "%Y%m%d%H%M%S%z")
print("Output Datetime object:", obj)
print('Data type:', type(obj))
Input string: 20151002151700-0800 Output Datetime object: 2015-10-02 15:17:00-08:00 Data type: <class 'datetime.datetime'>
Example 2: Readable Format
from datetime import datetime
date_string = 'Wed Mar 16 16:12:05 2016 +0800'
print("Input string:", date_string)
obj = datetime.strptime(date_string, '%a %b %d %H:%M:%S %Y %z')
print("Output Datetime object:", obj)
print('Data type:', type(obj))
Input string: Wed Mar 16 16:12:05 2016 +0800 Output Datetime object: 2016-03-16 16:12:05+08:00 Data type: <class 'datetime.datetime'>
Using dateutil.parser.parse() Function
The dateutil library provides a more flexible parse() function that automatically detects date formats without specifying format codes.
Example 1: Automatic Format Detection
from dateutil.parser import parse
date_string = 'Wed Mar 16 16:12:05 2016 +0800'
print("Input string:", date_string)
obj = parse(date_string)
print("Output Datetime object:", obj)
print('Data type:', type(obj))
Input string: Wed Mar 16 16:12:05 2016 +0800 Output Datetime object: 2016-03-16 16:12:05+08:00 Data type: <class 'datetime.datetime'>
Example 2: Handling Ambiguous Dates
The parse() function has parameters like dayfirst to handle ambiguous dates ?
from dateutil.parser import parse
date_string = '06/07/2005'
print("Input string:", date_string)
# Default: month first (US format)
obj1 = parse(date_string)
print("Default parsing:", obj1)
# Day first (European format)
obj2 = parse(date_string, dayfirst=True)
print("Day first parsing:", obj2)
Input string: 06/07/2005 Default parsing: 2005-06-07 00:00:00 Day first parsing: 2005-07-06 00:00:00
Comparison
| Method | Format Required? | Flexibility | Best For |
|---|---|---|---|
strptime() |
Yes | Limited | Known formats |
parse() |
No | High | Unknown formats |
Conclusion
Use datetime.strptime() when you know the exact format for better performance. Use dateutil.parser.parse() for unknown or varying formats as it automatically detects the format and handles ambiguous dates.
