Python : How to convert a timestamp string to a datetime object using datetime.strptime()
In this article we will discuss how to convert timestamp in different string formats to a datetime class object in Python.
String to datetime object using datetime.strptime()
Python’s datetime module provides a datetime class, which has a method to convert string to a datetime object i.e.
1 |
datetime.strptime(date_string, format) |
If accepts a string containing the timestamp and a format string containing the format codes representing the date time elements in date_string. It parses the string according to format codes and returns a datetime object created from it.
To use this import datetime class from datetime module i.e.
1 |
from datetime import datetime |
Let’s see some examples
Example 1:
Let’s convert a time string in format YYYY-MM-DDTHH::MM::SS.MICROS to a datetime object i.e.
1 2 3 4 5 |
datetimeObj = datetime.strptime('2018-09-11T15::11::45.456777', '%Y-%m-%dT%H::%M::%S.%f') print(datetimeObj) print(type(datetimeObj)) |
Output:
1 2 3 |
2018-09-11 15:11:45.456777 <class 'datetime.datetime'> |
Format string used here is : ‘%Y-%m-%dT%H::%M::%S.%f‘
Complete list of format code :
Format Codes | Description | Example |
---|---|---|
%d | Day of the month as a zero-padded decimal number | 01, 02, 03, 04 …, 31 |
%a | Weekday as abbreviated name | Sun, Mon, …, Sat |
%A | Weekday as full name | Sunday, Monday, …, Saturday |
%m | Month as a zero-padded decimal number | 01, 02, 03, 04 …, 12 |
%b | Month as abbreviated name | Jan, Feb, …, Dec |
%B | Month as full name | January, February, …, December |
%y | Year without century as a zero-padded decimal number | 00, 01, …, 99 |
%Y | Year with century as a decimal number | 0001, …, 2018, …, 9999 |
%H | Hour (24-hour clock) as a zero-padded decimal number | 01, 02, 03, 04 …, 23 |
%M | Minute as a zero-padded decimal number | 01, 02, 03, 04 …, 59 |
%S | Second as a zero-padded decimal number | 01, 02, 03, 04 …, 59 |
%f | Microsecond as a decimal number, zero-padded on the left | 000000, 000001, …, 999999 |
%I | Hour (12-hour clock) as a zero-padded decimal number | 01, 02, 03, 04 …, 12 |
%p | Locale’s equivalent of either AM or PM | AM , PM |
%j | Day of the year as a zero-padded decimal number | 01, 02, 03, 04 …, 366 |
Let’s see some other examples :
Example 2:
Let’s convert a timestamp string in format DD/MM/YYYY HH::MM::SS to a datetime object i.e.
1 2 3 |
datetimeObj = datetime.strptime('23/Jan/2018 14:12:22', '%d/%b/%Y %H:%M:%S') print(datetimeObj) |
Output:
1 |
2018-01-23 14:12:22 |
Example 3:
Create Date Time Object from date string only in ‘DD MMM YYYY‘ format
1 2 3 4 5 6 7 |
datetimeObj = datetime.strptime('29 Oct 2018', '%d %b %Y') # Get the date object from datetime object dateObj = datetimeObj.date() print(dateObj) print(type(dateObj)) |
Output:
1 2 |
2018-10-29 <class 'datetime.date'> |
Example 4:
Create datetime Object from time string only in ‘HH:MM:SS AP‘ format
1 2 3 4 5 6 7 |
datetimeObj = datetime.strptime('05:12:22 PM', '%I:%M:%S %p') # Get the time object from datetime object timeObj = datetimeObj.time() print(timeObj) print(type(timeObj)) |
Output:
1 2 |
17:12:22 <class 'datetime.time'> |
Example 5:
Create datetime Object from timestamp scattered in a text.
Some time our timestamp is embedded in a text like,
1 |
"On January the 5th of 2018 meet me at 5 PM" |
Let’s see how to convert timestamp in this string to a date time object with format codes mixed in text i.e.
1 2 3 4 5 |
textStr = "On January the 5th of 2018 meet me at 5 PM" datetimeObj = datetime.strptime(textStr, "On %B the %dth of %Y meet me at %I %p") print(datetimeObj) |
Output:
1 |
2018-01-05 17:00:00 |
Complete example is as follows,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
from datetime import datetime def main(): print("*** Convert timestamp String of format 'YYYY-MM-DDTHH::MM::SS.MICROS' to date time object ***") datetimeObj = datetime.strptime('2018-09-11T15::11::45.456777', '%Y-%m-%dT%H::%M::%S.%f') print(datetimeObj) print(type(datetimeObj)) print("*** Convert timestamp String of format 'DD/MM/YYYY HH:MM:SS' to date time object ***") # Convert String of format 'DD/MM/YYYY HH:MM:SS' to date time object datetimeObj = datetime.strptime('23/Jan/2018 14:12:22', '%d/%b/%Y %H:%M:%S') print(datetimeObj) print("*** Create DateTime Object from date string only in 'DD MMM YYYY' format ***") datetimeObj = datetime.strptime('29 Oct 2018', '%d %b %Y') # Get the date object from datetime object dateObj = datetimeObj.date() print(dateObj) print(type(dateObj)) print("*** Create DateTime Object from time string only in 'HH:MM:SS AP' format ***") datetimeObj = datetime.strptime('05:12:22 PM', '%I:%M:%S %p') # Get the time object from datetime object timeObj = datetimeObj.time() print(timeObj) print(type(timeObj)) print('*** Create DateTime Object from timestamp scattered in a text ***') textStr = "On January the 5th of 2018 meet me at 5 PM" datetimeObj = datetime.strptime(textStr, "On %B the %dth of %Y meet me at %I %p") print(datetimeObj) if __name__ == '__main__': main() |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
*** Convert timestamp String of format 'YYYY-MM-DDTHH::MM::SS.MICROS' to date time object *** 2018-09-11 15:11:45.456777 <class 'datetime.datetime'> *** Convert timestamp String of format 'DD/MM/YYYY HH:MM:SS' to date time object *** 2018-01-23 14:12:22 *** Create DateTime Object from date string only in 'DD MMM YYYY' format *** 2018-10-29 <class 'datetime.date'> *** Create DateTime Object from time string only in 'HH:MM:SS AP' format *** 17:12:22 <class 'datetime.time'> *** Create DateTime Object from timestamp scattered in a text *** 2018-01-05 17:00:00 |
Python Resources
- Best Python Tutorials on lists, dict, functions, iterators & many more.
- Data Analysis in Python using Pandas Dataframe - Top Tutorials
C++11 / C++14 Resources
- Best C++11 Tutorials on Topics like Smart Pointers, tuples, Unordered map / set, lambda function etc.
- C++11 Multi-threading Series
- C++ - Boost Library Tutorials
Leave a Reply