![]() |
VOOZH | about |
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.
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.
523000 2 35 5 1999-05-25 02:35:05.523000
datetime.strptime(time_data, format_data)
Parameters:
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.
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
In this example, we use Python’s time.strptime() function to convert date-time strings into structured struct_time objects.
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...
This example shows how to convert a date in string format into a datetime.date object using Python’s strptime() function.
2021-05-25
This example demonstrates how to convert a string containing both date and time into a Python datetime object using strptime().
2021-05-25 02:35:15
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.
%a: Abbreviated weekday name (Sun, Mon)%A: Full weekday name (Sunday, Monday)%w: Weekday as a decimal number, Sunday = 0 (0–6)%d: Day of the month, zero-padded (01, 02)%-d: Day of the month, no zero-padding (1, 2)%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)%y: Year without century, zero-padded (99, 00)%-y: Year without century, no zero-padding (0, 99)%Y: Year with century (2000, 1999)%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)%M: Minute, zero-padded (01, 59)%-M: Minute, no zero-padding (1, 59)%S: Second, zero-padded (01, 59)%-S: Second, no zero-padding (1, 59)%f: Microsecond, zero-padded to 6 digits (000000–999999)%z: UTC offset (+HHMM or −HHMM)%Z: Time zone name (UTC, IST)%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)%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)%%: Literal % character (%)