Python datetime.datetime.strptime() Method

Musfirah Waseem Jan 30, 2023
  1. Syntax of Python datetime.datetime.strptime() Method
  2. Example Codes: Working With the datetime.datetime.strptime() Method
  3. Example Codes: Convert the String Using the datetime.datetime.strptime() Method
  4. Example Codes: Sort the Date and Time Using Timestamp in the datetime.datetime.strptime() Method
  5. Example Codes: Convert the Format Using the datetime.datetime.strptime() Method
Python datetime.datetime.strptime() Method

Python datetime.datetime.strptime() method is an efficient way of converting any stated time in any format on any platform.

Syntax of Python datetime.datetime.strptime() Method

datetime.datetime.strptime(time_date, format_data)

Parameters

time_data It is the present time in the string data type.
format_data It is the data in DateTime format, which is then converted from time_data using the following codes
Code Description Example
%a Abbreviated weekday name Sun, Mon, Tue, Wed
%A Full weekday name Sunday, Monday
%w Weekday as decimal number 0,1,2,3,4,5,6
%d Day as a zero-padded decimal 01,02,03,04,05,06…
%-d Day as a decimal number 1,2,3,4,5,6…
%b Abbreviated month name Jan, Feb, Mar
%m Months as zero-padded decimal 01,02,03,04,05,06…
%-m Month number as a decimal 1,2,3,4,5,6…
%B Full month name January, February…
%y Year without century, zero-padded decimal 99,00,01..
%-y Year without century as a decimal 99,0,1..
%Y Year with century 1999,2000,2001..
%H 24 hour clock, zero-padded decimal 01,02,….23
%-H 24 hour clock as a decimal 1,2,3,….23
%I 12 hour clock, zero-padded decimal 01,02,….12
%-I 12 hour clock as a decimal 1,2,3,….12
%M Minutes as a zero-padded decimal 01,02,….59
%-M Minutes as a decimal 1,2,3,….59
%S Seconds as a zero-padded decimal 01,02,….59
%-S Seconds as a decimal 1,2,3,….59
%p locale’s AM or PM AM, PM
%Z Timezone name EST, UTC, GMT…
%z UTC offset in the form +HHMM or -HHMM +0112,-0112….
%j Day number of a year, zero-padded decimal 001,002,….365
%-j Day number in a year as a decimal 1,2,3,….365
%c locale’s date and time representation Mon Aug 29 07:06:05 2022
%x locale’s date representation 29/08/22
%X locale’s time representation 10:03:43
%f microsecond, zero-padded on the left side 000000…. 999999
%U Week number of a year (Sunday as first) 0,1,2…6
%W Week number of a year 00,01,…53

Return

The return type of this method is an object containing the current date and time in a specified format.

Example Codes: Working With the datetime.datetime.strptime() Method

import datetime

time_data = "29/08/22 02:35:5.523"

format_data = "%d/%m/%y %H:%M:%S.%f"

date_time = datetime.datetime.strptime(time_data, format_data)

print(date_time.microsecond)

print(date_time.hour)

print(date_time.minute)

print(date_time.second)

print(date_time)

Output:

523000
2
35
5
2022-08-29 02:35:05.523000

The above code shows only the attributes which we have specified.

Example Codes: Convert the String Using the datetime.datetime.strptime() Method

import datetime

date_string = "29 August, 2022"

print("The date in string format= ", date_string)

print("The type of `date_string` is= ", type(date_string))

date_object = datetime.datetime.strptime(date_string, "%d %B, %Y")

print("The date in date_time format is= ", date_object)

print("The type of date_object is= ", type(date_object))

Output:

The date in string format=  29 August, 2022
The type of `date_string` is=  <class 'str'>
The date in date_time format is=  2022-08-29 00:00:00
The type of date_object is=  <class 'datetime.datetime'>

Based on the string and the format code used, such as %d, %B, the method returns its equivalent datetime object.

Example Codes: Sort the Date and Time Using Timestamp in the datetime.datetime.strptime() Method

import datetime

time_data = [
    "09/05/99 02:35:8.023",
    "17/05/99 12:45:0.003",
    "27/05/99 07:35:5.523",
    "28/05/99 05:15:55.523",
]

format_data = "%d/%m/%y %H:%M:%S.%f"

for x in time_data:
    print(datetime.datetime.strptime(x, format_data))

Output:

1999-05-09 02:35:08.023000
1999-05-17 12:45:00.003000
1999-05-27 07:35:05.523000
1999-05-28 05:15:55.523000

In the above code, we got the time that follows a structure with all dates in a sequence.

Example Codes: Convert the Format Using the datetime.datetime.strptime() Method

import datetime

input = "2022/08/29"

format = "%Y/%m/%d"

date_time = datetime.datetime.strptime(input, format)

print(date_time.date())

Output:

2022-08-29

This method allows us to convert the time and date from string format to datetime format.

Musfirah Waseem avatar Musfirah Waseem avatar

Musfirah is a student of computer science from the best university in Pakistan. She has a knack for programming and everything related. She is a tech geek who loves to help people as much as possible.

LinkedIn

Related Article - Python DateTime