Python strptime

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 Python 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)

strptime function output

2018-12-30 00:00:00

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)
2018-12-31 00:00:00
2017-12-31 00:00:00

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)
Python strptime 3

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)

Print Date and Time from given Hours, Minutes, Seconds using strptime function.

1900-01-01 14:22:33
1900-01-01 10:45:59

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 function from time library

time.struct_time(tm_year=2018, tm_mon=12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=365, tm_isdst=-1)

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)

strptime function error output.

Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 4, in <module>
    datetime_value = datetime.strptime(dt_string, '%d %m %y')
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
  File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data '31/12/18' does not match format '%d %m %y'
>>>