English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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'>
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
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
The following table shows all the format codes you can use.
Command | Meaning | Example |
%a | Abbreviations of the day of the week. | Sun, Mon, ... |
%A | The full name of the day of the week. | Sunday, Monday, ... |
%w | The day of the week as a decimal number. | 0,1, ... ,6 |
%d | The day of the month represented as a decimal number padded with zeros. | 01, 02, ... ,31 |
%-d | The day of the month represented as a decimal number. | 1,2, ... ,30 |
%b | Abbreviations of the month. | Jan, Feb, ..., Dec |
%B | The full name of the month. | January, February, ... |
%m | The month represented as a decimal number padded with zeros. | 01, 02, ... ,12 |
%-m | The month represented as a decimal number. | 1,2, ... ,12 |
%y | The year without a century, represented as a decimal number padded with zeros. | 00, 01, ... ,99 |
%-y | The year without a century as a decimal number. | 0,1, ... ,99 |
%Y | The year represented as a decimal number with centuries. | 2013,2019and |
%H | Hour (24The 24-hour system), a decimal number padded with zeros. | 00, 01, ... ,23 |
%-H | Hour (24(12-hour clock) represented by a decimal number. | 0,1, ... ,23 |
%I | Hour (1212-hour clock), a zero-padded decimal number. | 01, 02, ... ,12 |
%-I | Hour (12(12-hour clock) represented by a decimal number. | 1 2 2 |
%p | The morning or afternoon in the language environment. | AM, PM |
%M | Minute, a zero-padded decimal number. | 00, 01, ... ,59 |
%-M | Represented by a decimal number. | 0,1, ... ,59 |
%S | The second zero-padded decimal number. | 00, 01, ... ,59 |
%-S | The second decimal digit. | 0,1, ... ,59 |
%f | Microseconds, a decimal number, padded with zeros on the left. | 000000-999999 |
%z | UTC offset, formatted as+ HHMM or-HHMM. | |
%Z | The time zone name. | |
%j | The day of the year, represented by a zero-padded decimal number. | 001, 002, ... ,366 |
%-j | The day of the year, represented by a decimal number. | 1,2, ... ,366 |
%U | The 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 |
%W | The 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 |
%c | The appropriate date and time representation in the language environment. | Mon Sep 30 07:06:05 2013 |
%x | The appropriate date representation in the language environment. | 13/9/30 |
%X | The appropriate time representation in the language environment. | 07:06:05 |
%% | The character '%%'. | % |
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()