Python strptime() Example
1. Introduction
Consider the case when you have a string containing date information and you would need to extract the date information like the day, month, year, and so many other information. Imagine a string like “30-10-2019” and you realize that the first part is the day, followed by the month, then the year.
Here is where the strptime()
method comes into play. It allows you to format any string into the right date and time information. It usually takes in the string you want to get the date information, then you provide the pattern in which the date information is provided in the string. You provide information like where the day, month, year, etc appear in the string.
1.1 Some Date And Time Patterns
Before we proceed any further, let us look at some keywords that are used when converting strings to date. These patterns enable us to specify what date information we want to get from the string object.
Pattern | Meaning |
%a | Abbreviated Weekday |
%A | Full Weekday Name |
%y | Year given as 2 digits. E.g. 95 |
%Y | Year given as the full 4 digits E.g. 1995 |
%b | Month Abbreviated |
%B | Full Month Name |
%H | Hour between 0 and 23 |
%M | Minute between 0 and 59 |
%S | Seconds between 0 and 59 |
1.2 Syntax
The syntax of the function is strptime(string, format)
It takes the string it wants to convert and also the format it wants to convert to. The output is of the type struct_time
The format is the most important part of the function because it provides you with ways to access the various date and time information from the string. We have a number of parameters which can be passed in our format string to help extract values from the date string.
1.3 Output
The output is of type struct_time
It has a number of components you would need to understand. The output looks like this:
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=78, tm_isdst=-1)
One of the most important things to also understand is that you would not get an output if the format is not well presented. When there is a mistake in how the format is presented, a ValueError
is raised. So it is important to check the format and string input carefully before seeking to checkout for the output.
Below are some of the components in the output.
tm_year | Year |
tm_month | Month; usually between 1 to 12 |
tm_mday | Day in the month; usually between 1 and 31 depending on the month |
tm_hour | Hour of the day, between 0 and 23 |
tm_min | Between 0 and 59 |
tm_weekday | Day of the week, between 0 and 6 starting from Monday |
tm_yday | Day in the year; between 1 to 366 |
2. Examples
So let us walk through an example for a better understanding. By the way, remember to import the time module first.
import time
2.1 Python strptime() – Example 1
Imagine you have the string “Wed 19, Mar, 2018”
and you want to convert this to date. The first thing you need to know is the various patterns that would be necessary to help fetch the information you need. So you have the shortened day of the week Wed
, the pattern to get the shortened day of the week is %a
, then we have the 19
, which is the day of the month, the pattern for that is %d
, then the shortened month name Mar
, then the next is the full year 2018
, and we would be using the %Y
pattern for that. So we can implement this as:
time.strptime('Tue 19, Mar 2018', "%a %d, %b %Y")
And the outcome is
time.struct_time(tm_year=2018, tm_mon=3, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=78, tm_isdst=-1)
2.2 Python strptime() – Example 2
Let us look at another example. Consider the string “30-10-19”
, the first information is the day of the month, so we use %d
to extract that. Next is the month in year, so we use %m
, lastly we have the last 2 digits of the year, so we use %y
.
So we implement this as:
time.strptime('30-10-19', "%d-%m-%y")
And the output is
time.struct_time(tm_year=2019, tm_mon=10, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=303, tm_isdst=-1)
2.3 Python strptime() – Example 3
Moving on, let us consider the example “Wed 20, 08, 2025, 23:52:18”
To get started with this example, we follow the same step as before.
String Information | Pattern Needed |
Wed | %a |
20 | %d |
08 | %m |
2025 | %Y |
23 | %H |
52 | %M |
18 | %S |
And the output is
So to implement this, we have
time.strptime('Wed 20, 08, 2025, 23:52:18', "%a %d, %m, %Y, %H:%M:%S")
And the output would be
time.struct_time(tm_year=2025, tm_mon=8, tm_mday=20, tm_hour=23, tm_min=52, tm_sec=18, tm_wday=2, tm_yday=232, tm_isdst=-1)
2.4 Python strptime() – Example 4
And for the final example, we have the string “Wednesday 24, August 1960”. We can also break this down as follows:
String Information | Pattern Needed |
Wednesday | %A |
24 | %d |
August | %B |
1960 | %Y |
So we implement as follows:
time.strptime('Wednesday 24, August, 1960', "%A %d, %B, %Y")
And our output is:
time.struct_time(tm_year=1960, tm_mon=8, tm_mday=24, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=237, tm_isdst=-1)
3. Conclusion
So in essence, the strptime
method allows to extract date and time information from string objects in Python. And as such, it generally makes dealing with time a little less stressful.
4. Download The Source Code
This was an example of how to use the strptime
function in Python.
You can download the full source code of this example here: Python strptime() Example