The Python strptime function is available in both the datetime and time module. This Python strptime function helps you to parse the datetime, date, or time in string representation.
This Python strptime function uses some directives to parse the string date. Remember, these are the same directives that we used in the strftime function to format dates and times. The syntax of this strptime in Python
datetime.strptime(string_value, format) # format using directives
Let me see the supporting Python strptime directives
- %a – Short Version of Weekday (Wed)
- %A – Full Version of Weekday (Wednesday)
- %b – Short Version of Month Name (Jan)
- %B – Full Version of Month Name (January)
- %c – Local Version of Date and Time
- %d – Day Number of a Month 01 to 31
- %f – Microseconds 000000 to 999999
- %H – Hours 00 to 23
- %I – Hours 00 to 12
- %j – Day Number of the Year 001 to 366
- %m – Month as a Number 01 to 12
- %M – Minutes from 00 to 59
- %p – AM or PM
- %S – Seconds from 00 to 59
- %U – Week Number of the Year from 00 to 53, where First Day of the Week = Sunday
- %w – Weekday as a Number from 0 to 6, Where 0 = Sunday
- %W – Week Number of the Year from 00 to 53, where First Day of the Week = Monday
- %x – Local Version of Date (05/23/19)
- %X – Local Version of Time (10:45:32)
- %y – Short Version of Year (19)
- %Y – Full Version of the Year (2019)
- %z – UTC Offset
- %Z – Timezone
Python strptime Examples
The strptime function converts the given string date in Day/Month/Year format to the local version of DateTime.
# Python strptime Example from datetime import datetime datetime_value = datetime.strptime('30/12/18', '%d/%m/%y') print(datetime_value)
This python strptime accepts the date in DayNumber MonthName and FullYear format and returns the Date and Time.
from datetime import datetime dt_string = '31 December 2018' datetime_value = datetime.strptime(dt_string, '%d %B %Y') print(datetime_value) dt_str = '31 December 17' dt_value = datetime.strptime(dt_str, '%d %B %y') print(dt_value)
Using the strptime function to convert the datetime string.
from datetime import datetime dt_string = '31/12/18 23:59:58' datetime_value = datetime.strptime(dt_string, '%d/%m/%y %H:%M:%S') print(datetime_value) dt_string = '10-12-2017 19:12:58' dt_value = datetime.strptime(dt_string, '%d-%m-%Y %H:%M:%S') print(dt_value)
Let me use this Python strptime to return the DateTime from the given Hours, Minutes, Seconds. Please refer to the strftime function in Python.
from datetime import datetime dt_string = '14:22:33' time_value = datetime.strptime(dt_string, '%H:%M:%S') print(time_value) dt_str = '10-45-59' t_value = datetime.strptime(dt_str, '%H-%M-%S') print(t_value)
Python strptime time library
You can also utilize the time library to use this strptime. In this time instance, we use the strptime function inside the time library.
import time datetime_value = time.strptime('31/12/18', '%d/%m/%y') print(datetime_value)
strptime example 3
If you specify the abbreviates in the wrong order or use the wrong format, then the strptime function throws an error.
# Python strptime Example from datetime import datetime dt_string = '31/12/18' datetime_value = datetime.strptime(dt_string, '%d %m %y') print(datetime_value)