English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Basic Tutorial

Python Flow Control

Python Functions

Python Data Types

Python File Operations

Python Objects and Classes

Python Date and Time

Advanced Python Knowledge

Python Reference Manual

Python strptime()

In this article, you will learn how to create datetime objects from strings (with the help of examples).

The strptime() method createsdatetimeObject.

Note:You cannot create a datetime object from each string. The string must be in a certain format.

Example1: String of the datetime object

from datetime import datetime
date_string = "21 June, 2018"
print("date_string =", date_string)
print("date_string data type =", type(date_string))
date_object = datetime.strptime(date_string, "%d %B, %Y")
print("date_object =", date_object)
print("date_object data type =", type(date_object))

The output when running the program is:

date_string = 21 June, 2018
date_string data type = <class 'str'>
date_object = 2018-06-21 00:00:00
date_object data type = <class 'datetime.datetime'>

How does strptime() work?

The strptime() class method has two parameters:

  • String (to be converted to datetime)

  • Format codes

Based on the used string and format codes, this method returns its equivalent datetime object.

In the above example:

Here,

  • %d-Represents the day of the month.Example: 01, 02, ... ,31

  • %B-The full name of the month.For example:January, February, etc.

  • %Y-The year is represented with four digits.For example: 2018,2019and

Example2: String of the datetime object

from datetime import datetime
dt_string = "12/11/2019 09:15:32"
# Date is dd / mm / yyyy format
dt_object1 = datetime.strptime(dt_string, "%d/%m/%Y %H:%M:%S")
print("dt_object1 =",1)
# Date is mm / dd / yyyy format
dt_object2 = datetime.strptime(dt_string, "%m/%d/%Y %H:%M:%S")
print("dt_object2 =",2)

The output when running the program is:

dt_object1 = 2019-11-12 09:15:32
dt_object2 = 2019-12-11 09:15:32

List of format codes

The following table shows all the format codes you can use.

CommandMeaningExample
%aAbbreviations of the day of the week.Sun, Mon, ...
%AThe full name of the day of the week.Sunday, Monday, ...
%wThe day of the week as a decimal number.0,1, ... ,6
%dThe day of the month represented as a decimal number padded with zeros.01, 02, ... ,31
%-dThe day of the month represented as a decimal number.1,2, ... ,30
%bAbbreviations of the month.Jan, Feb, ..., Dec
%BThe full name of the month.January, February, ...
%mThe month represented as a decimal number padded with zeros.01, 02, ... ,12
%-mThe month represented as a decimal number.1,2, ... ,12
%yThe year without a century, represented as a decimal number padded with zeros.00, 01, ... ,99
%-yThe year without a century as a decimal number.0,1, ... ,99
%YThe year represented as a decimal number with centuries.2013,2019and
%HHour (24The 24-hour system), a decimal number padded with zeros.00, 01, ... ,23
%-HHour (24(12-hour clock) represented by a decimal number.0,1, ... ,23
%IHour (1212-hour clock), a zero-padded decimal number.01, 02, ... ,12
%-IHour (12(12-hour clock) represented by a decimal number.1 2 2
%pThe morning or afternoon in the language environment.AM, PM
%MMinute, a zero-padded decimal number.00, 01, ... ,59
%-MRepresented by a decimal number.0,1, ... ,59
%SThe second zero-padded decimal number.00, 01, ... ,59
%-SThe second decimal digit.0,1, ... ,59
%fMicroseconds, a decimal number, padded with zeros on the left.000000-999999
%zUTC offset, formatted as+ HHMM or-HHMM. 
%ZThe time zone name. 
%jThe day of the year, represented by a zero-padded decimal number.001, 002, ... ,366
%-jThe day of the year, represented by a decimal number.1,2, ... ,366
%UThe week of the year (Sunday is the first day of the week). All days in the new year before the first Sunday are considered to be in the 0th week.00, 01, ... ,53
%WThe week of the year (Monday is the first day of the week). All days in the new year before the first Monday are considered to be in the 0th week.00, 01, ... ,53
%cThe appropriate date and time representation in the language environment.Mon Sep 30 07:06:05 2013
%xThe appropriate date representation in the language environment.13/9/30
%XThe appropriate time representation in the language environment.07:06:05
%%The character '%%'.%

ValueError in strptime()

If the string passed to the strptime() (the first parameter) and the format code (the second parameter) do not match, a ValueError will be obtained. For example:

from datetime import datetime
date_string = "12/11/2018"
date_object = datetime.strptime(date_string, "%d %m %Y")
print("date_object =", date_object)

If you run this program, an error will occur.

ValueError: time data'12/11/2018'does not match format '%d %m %Y'

Recommended reading: Python strftime()