Python DateTime - strptime() Function

The strptime stands for string parse time, and this function is part of the datetime module and is used for parsing strings representing time according to a specified format.

Syntax To Use the Function

  • date_string: The string containing the date and time information.
  • Format: A format string specifying the structure of the date_string.

Basic usage

Example 1:

Here is the basic usage of the strptime() function with the date

Program

Output:

2022-01-18 00:00:00

Explanation

Here, we specified the date and then formatted the string into the datetime object.

Example 2:

Here is an example of using the strptime to format the date and time

Program

Output:

2022-01-18 15:30:45

Explanation

The string is parsed using the strptime() method with a format string of "%Y-%m-%d %H:%M:%S" for both date and time components.

Example 3:

The example program handles the AM/PM information by converting the time into 24-hour format.

Program

Output:

2022-01-18 15:30:45

Explanation

This example demonstrates how to parse a string with AM/PM information. The format string "%Y-%m-%d %I:%M:%S %p" matches the structure of the input string.

Example 4:

Output:

2024-01-18 12:30:00
2023-02-20 08:45:00

Explanation

  • %a is used to parse the abbreviated weekday name (e.g., "Fri" for Friday).
  • %A is used to parse the full weekday name (e.g., "Monday").

String to datetime object

Here is a simple example to demonstrate the working of the strptime() function

Program

Output:

2024-01-18 12:30:45
<class 'datetime.datetime'>

Explanation

We have used the strptime() function to convert the string date to the original datetime object, specifying the particular format.

Considering Various Formats of Date

Example

Let us consider the program to consider the code to handle a variety of date string formats.

Program

Output:

2024-01-18 00:00:00
2024-01-18 00:00:00
2024-01-18 00:00:00
2024-01-18 12:30:00
2024-01-18 12:30:00

Directives

  • %Y: Year with century as a decimal number (e.g., 2024).
  • %y: Year without century as a zero-padded decimal number (00 to 99). Note that the century is determined by the platform's pivot year.
  • %m: Month as a zero-padded decimal number (01 to 12).
  • %d: Day of the month as a zero-padded decimal number (01 to 31).
  • %H: Hour (00 to 23).
  • %I: Hour (01 to 12).
  • %M: Minute (00 to 59).
  • %S: Second (00 to 59).
  • %A: Weekday as the full name (e.g., Monday).
  • %a: Weekday as an abbreviated name (e.g., Mon).
  • %B: Month as the full name (e.g., January).
  • %b or %h: Month as an abbreviated name (e.g., Jan).
  • %p: AM or PM.
  • %Z: Time zone name.
  • %j: Day of the year as a zero-padded decimal number (001 to 366).
  • %U: Week number of the year (Sunday as the first day of the week) as a zero-padded decimal number (00 to 53).
  • %W: Week number of the year (Monday as the first day of the week) as a zero-padded decimal number (00 to 53).
  • %c: Locale's appropriate date and time representation.
  • %x: Locale's appropriate date representation.
  • %X: Locale's appropriate time representation.
  • %%: A literal '%' character.

Conclusion

In conclusion, strptime() is a powerful method for converting date and time strings into datetime objects in Python. It provides flexibility through the format string, allowing you to parse a various date and time representations.