strptime() function in Python’s datetime module converts a date/time string into a datetime object, allowing you to work with textual dates as actual date objects.
Convert string to datetime and extract details
Python program to read datetime and get all time data using strptime. Here we are going to take time data in the string format and going to extract hours, minutes, seconds and milliseconds.
from datetime import datetime
t1 = "25/05/99 02:35:5.523"
f1 = "%d/%m/%y %H:%M:%S.%f"
date = datetime.strptime(t1, f1)
print(date.microsecond)
print(date.hour)
print(date.minute)
print(date.second)
print(date)
Output
523000 2 35 5 1999-05-25 02:35:05.523000
Syntax
datetime.strptime(time_data, format_data)
Parameters:
- time_data: The date/time as a string.
- format_data: The format of the string using datetime format codes.
Example 2: Convert a list of string timestamps
Python code that uses strptime. Here we are going to take time data in the string format and going to extract the time stamp in "%d/%m/%y %H:%M:%S.%f" format.
from datetime import datetime
t1 = ["25/05/99 02:35:8.023", "26/05/99 12:45:0.003",
"27/05/99 07:35:5.523", "28/05/99 05:15:55.523"]
f1 = "%d/%m/%y %H:%M:%S.%f"
for i in t1:
print(datetime.strptime(i, f1))
Output
1999-05-25 02:35:08.023000 1999-05-26 12:45:00.003000 1999-05-27 07:35:05.523000 1999-05-28 05:15:55.523000
Example 3. Using time.strptime() for structured time
In this example, we use Python’s time.strptime() function to convert date-time strings into structured struct_time objects.
import time
print(time.strptime('04/04/21 09:31:22', '%d/%m/%y %H:%M:%S'))
print(time.strptime('05/04/21 09:00:42', '%d/%m/%y %H:%M:%S'))
print(time.strptime('06/04/21 09:11:42', '%d/%m/%y %H:%M:%S'))
print(time.strptime('07/04/21 09:41:12', '%d/%m/%y %H:%M:%S'))
Output
time.struct_time(tm_year=2021, tm_mon=4, tm_mday=4, tm_hour=9, tm_min=31, tm_sec=22, tm_wday=6, tm_yday=94, tm_isdst=-1) time.struct_time(tm_year=2021, tm_mon=4, tm_mday=5, tm_hour=9, tm_min=0, tm_sec...
Example 4: Convert string date to datetime.date
This example shows how to convert a date in string format into a datetime.date object using Python’s strptime() function.
import datetime
i1 = '2021/05/25'
format = '%Y/%m/%d'
date = datetime.datetime.strptime(i1, format)
print(date.date())
Output
2021-05-25
Example 5: Convert string to datetime with full timestamp
This example demonstrates how to convert a string containing both date and time into a Python datetime object using strptime().
from datetime import datetime
d1 = "25/05/2021 02:35:15"
d2 = datetime.strptime(d1, "%d/%m/%Y %H:%M:%S")
print(d2)
Output
2021-05-25 02:35:15
How strptime() works
This function takes two arguments, a string in which some time is given and a format code, to change the string into, the string is changed to the DateTime object as per the list of codes given below.
Format Code
Weekday
%a:Abbreviated weekday name (Sun, Mon)%A:Full weekday name (Sunday, Monday)%w:Weekday as a decimal number, Sunday = 0 (0–6)
Day
%d:Day of the month, zero-padded (01, 02)%-d:Day of the month, no zero-padding (1, 2)
Month
%b:Abbreviated month name (Jan, Feb)%B:Full month name (January, February)%m:Month as zero-padded decimal (01, 02)%-m:Month as decimal number (1, 2)
Year
%y:Year without century, zero-padded (99, 00)%-y:Year without century, no zero-padding (0, 99)%Y:Year with century (2000, 1999)
Hour
%H:Hour (24-hour clock), zero-padded (01, 23)%-H:Hour (24-hour clock), no zero-padding (1, 23)%I:Hour (12-hour clock), zero-padded (01, 12)%-I:Hour (12-hour clock), no zero-padding (1, 12)%p:AM or PM (AM, PM)
Minute
%M: Minute, zero-padded (01, 59)%-M: Minute, no zero-padding (1, 59)
Second
%S: Second, zero-padded (01, 59)%-S: Second, no zero-padding (1, 59)
Microsecond
%f: Microsecond, zero-padded to 6 digits (000000–999999)
Time Zone
%z:UTC offset (+HHMM or −HHMM)%Z:Time zone name (UTC, IST)
Day / Week of Year
%j:Day of the year, zero-padded (001–365)%-j: Day of the year, no zero-padding (1–365)%U: Week number of the year, Sunday as first day (0–6)%W: Week number of the year, Monday as first day (00–53)
Locale-specific formats
%c:Locale’s full date and time (Mon Sep 30 07:06:05 2013)%x: Locale’s date format (11/30/98)%X: Locale’s time format (10:03:43)
Special
%%: Literal%character (%)