How to convert a Python date string to a date object?


In this article we convert a date string to a date object. We use the strptime() method to convert a date string to a date object.

Strptime()

The strptime() method takes the date as an input and converts it into a date object. Note that you cannot convert any string into a datetime object. The string should be in a certain format.

Syntax

The syntax of the strptime() method is shown in this section.

datetime.strptime(time_data, format_data)

Where,

time_date – it is the time present in string format.

format_date – it is the data(string) present in datetime format which is converted from time_data using this function.

Few important format codes

Format Code Meaning Example
%a Shortened weekday name. Mon, Tue,….
%A Complete weekday name. Monday, Tuesday,….
%w Weekday as a decimal value. 0,1,2….
%d Day of the month as a zero-padded decimal. 01,02,…,30
%-d Day of the month as a decimal number. 1,2,…….,30
%b Shortend Month name. Jan,Feb,…
%B Complete Month name. January, February,….
%m Month as a zero-padded decimal number. 01,02,03,……,12
%-m Month as a decimal number. 1,2,3,…….,12.
%y Year without century as a zero-padded decimal number. 00,01,….
%Y Year with century as a decimal number. 2001,2002,….
%-y Year without century as a decimal number. 0,1,2,3,…….
%H Hour(24-hour clock) as a zero-padded decimal number. 01,02,…..,24
%-H Hour(24 hour clock) as a decimal number. 1,2,….,24.
%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 AM or PM AM,PM.
%M Minute as a zero padded decimal number. 00,01,02,….,59
%-M Minute as a decimal number. 1,2,…..,59.
%S Second as a zero-padded decimal number. 01,02,….,59
%-S Second as a decimal number. 1,2,…..,59.
%f Microsecond as a decimal number, zero padded on the left side 000000,000001,….999999
%j Day of the year as a zero-padded decimal number. 001, 002, ..., 366

Example 1

In the following example we convert a string date to a datetime object by using the strptime. And we also extract the microsecond, minutes, and seconds.

from datetime import datetime time_data = "23/01/01 04:35:7.123" format_data = "%d/%m/%y %H:%M:%S.%f" date = datetime.strptime(time_data, format_data) print("Microsecond:",date.microsecond) print("Hour:",date.hour) print("Minute:",date.minute) print("Second:",date.second) print("The date is:",date)

Output

The output received is as follows.

Microsecond: 123000
Hour: 4
Minute: 35
Second: 7
The date is: 2001-01-23 04:35:07.123000

Example 2

The module is imported in this first example, and the input DateTime string is provided. Now we use the strptime() method to get the needed format and date() to retrieve the date from DateTime.

import datetime date_str = '29122017' # The date - 29 Dec 2017 format_str = '%d%m%Y' # The format datetime_obj = datetime.datetime.strptime(date_str, format_str) print(datetime_obj.date())

Output

The output produced is shown below.

2017-12-29

Updated on: 02-Nov-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements