Python 3 – time strptime() Method
Python is widely used for its simplicity and ease of use. With its built-in libraries and modules, developers can easily perform complex tasks with simplicity. In this article, we will be discussing the strptime()
method in the time
module of Python 3.
Introduction to Strptime()
The strptime()
method is an inbuilt datetime class method in Python. It is used to convert a string representing a time/date into a datetime
object. The strptime()
method in Python stands for “string parse time”, which is the opposite of the strftime()
method. The strptime()
method takes two arguments where the first argument represents the string we want to parse, and the second argument specifies the format of that string.
Syntax of Strptime()
Here is the syntax of strptime()
method:
datetime.strptime(date_string, format)
Examples
Let’s first take a look at some examples of strptime() method to understand its functionality.
import datetime
date_string = "21 June, 2021"
date_object = datetime.datetime.strptime(date_string, "%d %B, %Y")
print("Date: ", date_object.date())
Output:
Date: 2021-06-21
Here, we have imported datetime
module and created a variable date_string
which contains the date we want to convert. Then, we have called the strptime()
method and passed the date_string
and its format as arguments. In the second line of output, we have printed the formatted date.
Let’s take a look at another example:
import datetime
date_string = "July 7, 2021"
date_object = datetime.datetime.strptime(date_string, "%B %d, %Y")
print("Date: ", date_object.date())
Output:
Date: 2021-07-07
Here, we have created a variable date_string and passed the date we want to convert. Then, we have used the strptime()
function to parse the date_string, and its format as arguments.
Format Codes
Here are some of the format codes we can use with strptime()
method:
Code | Meaning | Expressed as |
---|---|---|
%a | Weekday | Mon, Tue etc. |
%A | Weekday Name | Monday, Tuesday etc. |
%b | Month | Jan, Feb etc. |
%B | Month Name | January, February etc. |
%c | Locale Date and Time | Tue Aug 16 21:30:00 1988 |
%d | Day of the Month | 01, 02 etc. |
%H | Hour (24-Hour Format) | 00, 01 … 23 |
%I | Hour (12-Hour Format) | 01, 02 … 12 |
%j | Day of the Year | 001, 002 … 366 |
%m | Month | 01, 02 … 12 |
%M | Minute | 00, 01 … 59 |
%p | Meridian indicator | AM or PM |
%S | Second | 00, 01 … 59 |
%U | week number of the year (Sunday as the first day of the week) | 00, 01, … 53 |
%w | Weekday as a decimal number (0-6, 0 is Sunday) | 0, 1 … 6 |
%W | week number of the year (Monday as the first day of the week) | 00, 01, … 53 |
%x | Locale Date | Locale specific |
%X | Locale Time | Locale specific |
%y | Year (2 digits) | 00, 01, … 99 |
%Y | Year (4 digits) | 0001, 0002 … 2024 |
%z | UTC Offset | +0627 |
%Z | Timezone Name | UTC, EST etc. |
%% | a literal ‘%’ character | % |
Conclusion
In conclusion, the strptime()
method in Python is very useful when dealing with dates and time. With its simple syntax and a list of format codes, it makes date and time parsing an easy task. This method can save a lot of time in situations where data needs to be parsed and manipulated. Additionally, this method is versatile enough to be used in many different applications. Overall, the strptime()
method is a valuable tool in any Python programmer’s arsenal.