Python strptime()

By | August 6, 2019

In this tutorial, we will discuss how can we use a string to make a datetime object.

Python strptime()

strftime() is a datetime method which is used to convert a string to a datetime object. It is opposite of strftime() method.

We often use strftime() method to bring the default formatting of datetime module which is YYYY-mm-dd HH:MM:SS

Let’s understand it with an 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 which is actually a string that contains a date. With the help of datetime strptime() method and str_date, we create a datetime object date_obj.

You can see that we use the format code such as %d, %B and %Y in 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 Year without century as a zero-padded decimal number. 00, 01, …, 99
%-y Year without century as a decimal number. 0, 1, …, 99
%Y 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 to be 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 to be 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 that the string format and the format code must be represented in the same way so each format code can relate to the appropriate string.

If the format code and string that have passed does 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'

Python Tutorials


Introduction

Flow Control

Functions

Datatypes

File Handling

Object & Class

Additional Tutorials

Leave a Reply

Your email address will not be published. Required fields are marked *