In this tutorial, we will learn how to convert a timestamp string to a datetime object using datetime.strptime(). Also, you can understand how to to create a datetime object from a string in Python with examples below.
- String to a DateTime object using datetime.strptime()
- Format Code List
- How strptime() works?
- Examples of converting a Time String in the format codes using strptime() method
String to a DateTime object using datetime.strptime()
Thestrptime()
method generates a datetime object from the given string.
Datetime module provides a datetime class that has a method to convert string to a datetime object.
Syntax:
datetime.strptime(date_string, format)
So in the above syntax, you can see that it accepts a string containing a timestamp. It parses the string according to format codes and returns a datetime object created from it.
First import datetime class from datetime module to use this,
from datetime import datetime
Also Read:
Complete Format Code List
Format Codes | Description | Example |
---|---|---|
%d | Day of the month as a zero-padded decimal number | 01, 02, 03, 04 …, 31 |
%a | Weekday as the 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 an abbreviated name | Jan, Feb, …, Dec |
%B | Month as full name | January, February, …, December |
%y | A Year without century as a zero-padded decimal number | 00, 01, …, 99 |
%Y | A Year with a 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 |
How strptime() works?
In thestrptime()
class method, it takes two arguments:
- string (that be converted to datetime)
- format code
In the accordance with the string and format code used, the method returns its equivalent datetime object.
Let’s see the following example, to understand how it works:
where,
%d
– Represents the day of the month. Example: 01, 02, …, 31
%B
– Month’s name in full. Example: January, February etc.
%Y
– Year in four digits. Example: 2018, 2019 etc.
Examples of converting a Time String in the format codes using strptime() method
Just have a look at the few examples on how to convert timestamp string to a datetime object using datetime.strptime() in Python and gain enough knowledge on it.
Example 1:
Let’s take an example,
from datetime import datetime datetimeObj = datetime.strptime('2021-05-17T15::11::45.456777', '%Y-%m-%dT%H::%M::%S.%f') print(datetimeObj) print(type(datetimeObj))
Output:
2021-05-17 15:11:45.456777 <class 'datetime.datetime'>
So in the above example, you can see that we have converted a time string in the format “YYYY-MM-DDTHH::MM::SS.MICROS” to a DateTime object.
Let’s take another example,
Example 2:
from datetime import datetime datetimeObj = datetime.strptime('17/May/2021 14:12:22', '%d/%b/%Y %H:%M:%S') print(datetimeObj) print(type(datetimeObj))
Output:
2021-05-17 14:12:22 <class 'datetime.datetime'>
So this is the other way to show timestamp here we have converted a time string in the format “DD/MM/YYYY HH::MM::SS” to a datetime object.
Example 3:
If we want to show the only date in this format “DD MMM YYYY”. We do like this,
from datetime import datetime datetimeObj = datetime.strptime('17 May 2021', '%d %b %Y') # Get the date object from datetime object dateObj = datetimeObj.date() print(dateObj) print(type(dateObj))
Output:
2021-05-17 <class 'datetime.date'>
Example 4:
So if we want to show only time “‘HH:MM:SS AP‘” in this format. We will do like that,
from datetime import datetime datetimeObj = datetime.strptime('08:12:22 PM', '%I:%M:%S %p') # Get the time object from datetime object timeObj = datetimeObj.time() print(timeObj) print(type(timeObj))
Output:
20:12:22 <class 'datetime.time'>
Example 5:
If we want to show our timestamp in text format. We will execute like that,
from datetime import datetime textStr = "On January the 17th of 2021 meet me at 8 PM" datetimeObj = datetime.strptime(textStr, "On %B the %dth of %Y meet me at %I %p") print(datetimeObj)
Output:
2021-01-17 20:00:00
Conclusion:
So in the above tutorial, you can see that we have shown different methods of how to convert a timestamp string to a datetime object using datetime.strptime(). Thank you!