Python strptime

The Python strptime function is available in both the datetime and time modules. It 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 is

datetime.strptime(string_value, format) # format using directives

Let me see the supporting 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

This function converts the given string date in Day/Month/Year format to the local version of DateTime.

 
from datetime import datetime as dm
 
dvalue = dm.strptime('30/12/18', '%d/%m/%y')
 
print(dvalue)
2018-12-30 00:00:00

This strptime accepts the date in DayNumber MonthName and FullYear format and returns the Date and Time.

dt_string = '31 December 2018'
value = dm.strptime(dt_string, '%d %B %Y')
print(value)
 
dt_str = '31 December 17'
dt_value = dm.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 as stp
 
dt_str = '31/12/18 23:59:58'
dt_value = stp.strptime(dt_str, '%d/%m/%y %H:%M:%S')
print(dt_value)
 
dt_str2 = '10-12-2017 19:12:58'
dt_value2 = stp.strptime(dt_str2, '%d-%m-%Y %H:%M:%S')
print(dt_value2)
Python strptime 3

Let me use this Python strptime to return the DateTime from the given Hours, Minutes, and Seconds.

dt_string = '14:22:33'
time_value = stp.strptime(dt_string, '%H:%M:%S')
print(time_value)
 
dt_str = '10-45-59'
t_value = stp.strptime(dt_str, '%H-%M-%S')
print(t_value)

Print Date and Time from given Hours, Minutes, and Seconds using this function. Please refer to the strftime in Python.

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

time library

You can also utilize the time library to use this function. In this time instance, we use it inside the time library.

import time
 
dvalue = time.strptime('31/12/18', '%d/%m/%y')
print(dvalue)
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)

If you specify the abbreviates in the wrong order or use the wrong format, then the function throws an error.

from datetime import datetime as rdm
 
dt_string = '31/12/18'
dval = rdm.strptime(dt_string, '%d %m %y')
 
print(dval)
Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 4, in <module>
    ........
  ......
......
......
>>>