|
| 1 | +--- |
| 2 | +Title: '.strptime()' |
| 3 | +Description: 'Parses a string representing a date/time into a struct_time object according to a specified format.' |
| 4 | +Subjects: |
| 5 | + - 'Code Foundations' |
| 6 | + - 'Computer Science' |
| 7 | +Tags: |
| 8 | + - 'Date' |
| 9 | + - 'Methods' |
| 10 | + - 'Time' |
| 11 | +CatalogContent: |
| 12 | + - 'learn-python-3' |
| 13 | + - 'paths/computer-science' |
| 14 | +--- |
| 15 | + |
| 16 | +**`.strptime()`** is a method from Python's [time](https://www.codecademy.com/resources/docs/python/time-module) module. It converts a date and/or time string into a corresponding `struct_time` object, based on the format directives provided. |
| 17 | + |
| 18 | +> > **Note:** `strptime()` with the 'p' for 'parse' is for reading a string, not writing one. |
| 19 | +
|
| 20 | +## Syntax |
| 21 | + |
| 22 | +```pseudo |
| 23 | +time.strptime(string, format) |
| 24 | +``` |
| 25 | + |
| 26 | +**Parameters:** |
| 27 | + |
| 28 | +- `string` (str): The date and/or time string to be parsed and converted. |
| 29 | +- `format` (str): The format string specifying the expected format of the first parameter `string`. |
| 30 | + |
| 31 | +**Return value:** |
| 32 | + |
| 33 | +Returns a `struct_time` object with the parsed date and time. |
| 34 | + |
| 35 | +The `format` parameter uses specific directives to represent the expected format of `string`. These directives are the same ones as those used by [`strftime()`](https://www.codecademy.com/resources/docs/python/time-module/strftime), and some common examples include: |
| 36 | + |
| 37 | +| Directive | Description | Example/Range | |
| 38 | +| --------- | --------------------------- | --------------- | |
| 39 | +| `%Y` | Four-digit year | 2025, 2026, ... | |
| 40 | +| `%y` | Two-digit year (25 -> 2025) | 00 - 99 | |
| 41 | +| `%m` | Month number | 01 - 12 | |
| 42 | +| `%d` | Day of the month | 01 - 31 | |
| 43 | +| `%H` | Hour (24-hour format) | 00 - 23 | |
| 44 | +| `%M` | Minute | 00 - 59 | |
| 45 | +| `%S` | Second | 00 - 61\* | |
| 46 | +| `%w` | Weekday | 0(Sun) - 6(Sat) | |
| 47 | +| `%j` | Day of the year | 001 - 366 | |
| 48 | +| `%b` | Abbreviated month name | Jan, Feb, ... | |
| 49 | + |
| 50 | +> > **Note:** Value `61` is only supported for historical reasons (leap seconds). |
| 51 | +
|
| 52 | +## Example |
| 53 | + |
| 54 | +The following example shows how to convert a date string into a `struct_time` object using `time.strptime()`: |
| 55 | + |
| 56 | +```py |
| 57 | +import time |
| 58 | + |
| 59 | +# Define the date string and its corresponding format |
| 60 | +date_string = "16 Dec 97" |
| 61 | +format_string = "%d %b %y" |
| 62 | + |
| 63 | +# Parse the date string into a struct_time object |
| 64 | +converted_time = time.strptime(date_string, format_string) |
| 65 | +print(converted_time) |
| 66 | +``` |
| 67 | + |
| 68 | +An example output of this code is: |
| 69 | + |
| 70 | +```shell |
| 71 | +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) |
| 72 | +``` |
| 73 | + |
| 74 | +## Codebyte Example |
| 75 | + |
| 76 | +This example shows how to parse a date string and access information from the `struct_time` object: |
| 77 | + |
| 78 | +```codebyte/python |
| 79 | +import time |
| 80 | +
|
| 81 | +date_string = "21 June 2025" |
| 82 | +format_string = "%d %B %Y" |
| 83 | +
|
| 84 | +converted_time = time.strptime(date_string, format_string) |
| 85 | +
|
| 86 | +# Access individual elements |
| 87 | +year = converted_time.tm_year |
| 88 | +month = converted_time.tm_mon |
| 89 | +day = converted_time.tm_mday |
| 90 | +
|
| 91 | +print(f"Year: {year}, Month: {month}, Day: {day}") |
| 92 | +``` |
0 commit comments