Skip to content

Commit d248598

Browse files
Add strptime.md file
1 parent 13a434d commit d248598

File tree

1 file changed

+48
-0
lines changed
  • content/python/concepts/dates/terms/strptime

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
Title: '.strptime()'
3+
Description: 'Return a datetime corresponding to date_string, parsed according to format.'
4+
Subjects:
5+
- 'Python'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Date'
9+
- 'Time'
10+
- 'Strings'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`.strptime()`** is a method included in the `datetime` module. It is used to parse a string representing a date and/or time and convert it into a `datetime` object using a specified format.
17+
18+
## Syntax
19+
20+
```python
21+
from datetime import datetime
22+
23+
datetime.strptime(date_string, format)
24+
```
25+
26+
### Parameters:
27+
28+
- `date_string` (str): The string representing the date and/or time.
29+
- `format` (str): The format that defines the structure of `date_string`. This uses the directives from the `datetime` module (e.g., `%Y` for a four-digit year, `%m` for a two-digit month).
30+
31+
### Returns:
32+
33+
- A `datetime` object corresponding to the parsed date and time.
34+
35+
## Codebyte Example
36+
37+
```codebyte/python
38+
from datetime import datetime
39+
40+
# Define the date-time string and format
41+
datetime_string = "27/12/2024 15:30:00"
42+
datetime_format = "%d/%m/%Y %H:%M:%S"
43+
44+
# Parse the string into a datetime object
45+
dt_object = datetime.strptime(datetime_string, datetime_format)
46+
47+
print(dt_object) # Output: 2024-12-27 15:30:00
48+
```

0 commit comments

Comments
 (0)