In this tutorial, we will discuss how we can use a string to make a datetime object.
Python strptime()
strptime() is a datetime method to convert a string to a datetime object. It is the opposite of the strftime() method. We often use the strftime() method to bring the default formatting of the datetime module, YYYY-mm-dd HH:MM:SS.
Example
from datetime import datetime
str_date = '04 August 2019'
print('Value of str_date is:', str_date)
print('Type of str_date is:', type(str_date))
print('+++++++++++++ After using strptime() method++++++++++++')
date_obj = datetime.strptime(str_date, "%d %B %Y")
print('Value of date_obj is:',date_obj)
print('Type of date_obj is:',type(date_obj))
Output
Value of str_date is: 04 August 2019
Type of str_date is: <class 'str'>
+++++++++++++ After using strptime() method++++++++++++
Value of date_obj is: 2019-08-04 00:00:00
Type of date_obj is: <class 'datetime.datetime'>
Behind the code
In the above example, first, we defined a variable str_date, a string containing a date. With the help of the datetime strptime() method and str_date, we create a datetime object date_obj .
We use format codes, such as %d, %B, and %Y, in the strptime() method. They are there to tell the method which character should be treated like a day, year, or month.
Working of strptime() method
strptime() method accepts two arguments:
- string
- format code
The string contains the date formation in the string, whereas the format code tells the method which string or character should consider month, year, day, hour, minute, second, etc. The format code should be written in the same way as the string date and time is represented.
Example
from datetime import datetime
str_date = 'date 04/08/2019 time 20:45:40'
print('Value of str_date is:', str_date)
print('Type of str_date is:', type(str_date))
print('+++++++++++++ After using strptime() method++++++++++++')
date_obj = datetime.strptime(str_date, "date %d/%m/%Y time %H:%M:%S")
print('Value of date_obj is:',date_obj)
print('Type of date_obj is:',type(date_obj))
Output
Value of str_date is: date 04/08/2019 time 20:45:40
Type of str_date is: <class 'str'>
+++++++++++++ After using strptime() method++++++++++++
Value of date_obj is: 2019-08-04 20:45:40
Type of date_obj is: <class 'datetime.datetime'>
strptime() format Codes
The format codes are fixed, so the date you mention in the string must be related to the following format codes:
Format code | Description | Example |
%a | Abbreviated weekday name. | Sun, Mon, ... |
%A | Full weekday name. | Sunday, Monday, ... |
%w | Weekday as a decimal number. | 0, 1, ..., 6 |
%d | Day of the month as a zero-padded decimal. | 01, 02, ..., 31 |
%-d | Day of the month as a decimal number. | 1, 2, ..., 30 |
%b | Abbreviated month name. | Jan, Feb, ..., Dec |
%B | Full month name. | January, February, ... |
%m | Month as a zero-padded decimal number. | 01, 02, ..., 12 |
%-m | Month as a decimal number. | 1, 2, ..., 12 |
%y | A year without a century as a zero-padded decimal number. | 00, 01, ..., 99 |
%-y | A year without a century as a decimal number. | 0, 1, ..., 99 |
%Y | The year with century as a decimal number. | 2013, 2019 etc. |
%H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, ..., 23 |
%-H | Hour (24-hour clock) as a decimal number. | 0, 1, ..., 23 |
%I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, ..., 12 |
%-I | Hour (12-hour clock) as a decimal number. | 1, 2, ... 12 |
%p | Locale’s AM or PM. | AM, PM |
%M | Minute as a zero-padded decimal number. | 00, 01, ..., 59 |
%-M | Minute as a decimal number. | 0, 1, ..., 59 |
%S | Second as a zero-padded decimal number. | 00, 01, ..., 59 |
%-S | Second as a decimal number. | 0, 1, ..., 59 |
%f | Microsecond as a decimal number, zero-padded on the left. | 000000 - 999999 |
%z | UTC offset in the form +HHMM or -HHMM. | |
%Z | Time zone name. | |
%j | Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 |
%-j | Day of the year as a decimal number. | 1, 2, ..., 366 |
%U | Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered in week 0. | 00, 01, ..., 53 |
%W | Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered in week 0. | 00, 01, ..., 53 |
%c | Locale’s appropriate date and time representation. | Mon Sep 30 07:06:05 2013 |
%x | Locale’s appropriate date representation. | 09/30/13 |
%X | Locale’s appropriate time representation. | 07:06:05 |
%% | A literal '%' character. | % |
Value Error in strptime()
As we have discussed, the string format and the format code must be represented similarly so each format code can relate to the appropriate string. If the format code and string that have passed do not match, it will throw a ValueError.
Example
from datetime import datetime
str_date = 'date 04/08/2019 time 20:45:40'
date_obj = datetime.strptime(str_date, " %d/%m/%Y %H:%M:%S")
print('Value of date_obj is:',date_obj)
Output
ValueError: time data 'date 04/08/2019 time 20:45:40' does not match format ' %d/%m/%Y %H:%M:%S'
Conclusion
This was all about the Python strptime(). It is a datetime method for converting a string into a datetime object. In this article, we have mentioned one example to understand better how the Python strptime() method works. Also, you can refer to the table mentioned above for strptime() format codes. Finally, we discussed the value error you might encounter in the Python strptime() method.
People are also reading:
Leave a Comment on this Post