Python .strptime()
.strptime() is a method from Python’s time module. It converts a date and/or time string into a corresponding struct_time object, based on the format directives provided.
Note:
strptime()with the ‘p’ for ‘parse’ is for reading a string, not writing one.
Syntax
time.strptime(string, format)
Parameters:
string(str): The date and/or time string to be parsed and converted.format(str): The format string specifying the expected format of the first parameterstring.
Return value:
Returns a struct_time object with the parsed date and time.
The format parameter uses specific directives to represent the expected format of string. These directives are the same ones as those used by strftime(), and some common examples include:
| Directive | Description | Example/Range |
|---|---|---|
%Y |
Four-digit year | 2025, 2026, … |
%y |
Two-digit year (25 -> 2025) | 00 - 99 |
%m |
Month number | 01 - 12 |
%d |
Day of the month | 01 - 31 |
%H |
Hour (24-hour format) | 00 - 23 |
%M |
Minute | 00 - 59 |
%S |
Second | 00 - 61* |
%w |
Weekday | 0(Sun) - 6(Sat) |
%j |
Day of the year | 001 - 366 |
%b |
Abbreviated month name | Jan, Feb, … |
Note: Value
61is only supported for historical reasons (leap seconds).
Example
The following example shows how to convert a date string into a struct_time object using time.strptime():
import time# Define the date string and its corresponding formatdate_string = "16 Dec 97"format_string = "%d %b %y"# Parse the date string into a struct_time objectconverted_time = time.strptime(date_string, format_string)print(converted_time)
An example output of this code is:
time.struct_time(tm_year=1997, tm_mon=12, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=1, tm_yday=350, tm_isdst=-1)
Codebyte Example
This example shows how to parse a date string and access information from the struct_time object:
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours