From d248598ac4a8a3cf5fdb885dcd8b2b832031b411 Mon Sep 17 00:00:00 2001 From: EduardoGallego94 Date: Fri, 27 Dec 2024 19:55:14 +0100 Subject: [PATCH 01/10] Add strptime.md file --- .../concepts/dates/terms/strptime/strptime.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 content/python/concepts/dates/terms/strptime/strptime.md diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md new file mode 100644 index 00000000000..6e54f067f41 --- /dev/null +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -0,0 +1,48 @@ +--- +Title: '.strptime()' +Description: 'Return a datetime corresponding to date_string, parsed according to format.' +Subjects: +- 'Python' +- 'Computer Science' +Tags: +- 'Date' +- 'Time' +- 'Strings' +CatalogContent: +- 'learn-python-3' +- 'paths/computer-science' +--- + +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. + +## Syntax + +```python +from datetime import datetime + +datetime.strptime(date_string, format) +``` + +### Parameters: + +- `date_string` (str): The string representing the date and/or time. +- `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). + +### Returns: + +- A `datetime` object corresponding to the parsed date and time. + +## Codebyte Example + +```codebyte/python +from datetime import datetime + +# Define the date-time string and format +datetime_string = "27/12/2024 15:30:00" +datetime_format = "%d/%m/%Y %H:%M:%S" + +# Parse the string into a datetime object +dt_object = datetime.strptime(datetime_string, datetime_format) + +print(dt_object) # Output: 2024-12-27 15:30:00 +``` \ No newline at end of file From 1a6089d3584eccea6b9b80f259603ebe8e54c40e Mon Sep 17 00:00:00 2001 From: EduardoGallego94 Date: Mon, 30 Dec 2024 19:14:59 +0100 Subject: [PATCH 02/10] Update content/python/concepts/dates/terms/strptime/strptime.md Co-authored-by: Mamta Wardhani --- content/python/concepts/dates/terms/strptime/strptime.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index 6e54f067f41..889bf37d9ab 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -1,6 +1,6 @@ --- Title: '.strptime()' -Description: 'Return a datetime corresponding to date_string, parsed according to format.' +Description: 'Returns a datetime object that represents the parsed date and time from the given string, based on the specified format.' Subjects: - 'Python' - 'Computer Science' From 82ad5d8df9a7421bdacb113374a3d57a4c36fe63 Mon Sep 17 00:00:00 2001 From: EduardoGallego94 Date: Mon, 30 Dec 2024 19:15:16 +0100 Subject: [PATCH 03/10] Update content/python/concepts/dates/terms/strptime/strptime.md Co-authored-by: Mamta Wardhani --- content/python/concepts/dates/terms/strptime/strptime.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index 889bf37d9ab..9940f74a8c0 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -13,7 +13,7 @@ CatalogContent: - 'paths/computer-science' --- -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. +The **`.strptime()`** is a method included in the [`datetime`](https://www.codecademy.com/resources/docs/python/dates) 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. ## Syntax From 2454a0a3ff567a1b01295416aa3c1f7ec7e92e25 Mon Sep 17 00:00:00 2001 From: EduardoGallego94 Date: Mon, 30 Dec 2024 19:17:20 +0100 Subject: [PATCH 04/10] Update content/python/concepts/dates/terms/strptime/strptime.md Co-authored-by: Mamta Wardhani --- content/python/concepts/dates/terms/strptime/strptime.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index 9940f74a8c0..bff906f9b6e 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -25,7 +25,7 @@ datetime.strptime(date_string, format) ### Parameters: -- `date_string` (str): The string representing the date and/or time. +- `date_string` (str): The string representing the date and/or time to be parsed.. - `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). ### Returns: From 1bce30addbaf78937f7d105cc9ba5ee277a686d7 Mon Sep 17 00:00:00 2001 From: EduardoGallego94 Date: Mon, 30 Dec 2024 19:17:35 +0100 Subject: [PATCH 05/10] Update content/python/concepts/dates/terms/strptime/strptime.md Co-authored-by: Mamta Wardhani --- content/python/concepts/dates/terms/strptime/strptime.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index bff906f9b6e..2aff27b2db5 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -26,7 +26,7 @@ datetime.strptime(date_string, format) ### Parameters: - `date_string` (str): The string representing the date and/or time to be parsed.. -- `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). +- `format` (str): A string that defines the structure of `date_string` using format codes from the `datetime` module (e.g., `%Y` for a four-digit year, `%m` for a two-digit month). ### Returns: From 93aa5c29c3799cf1f6c15bc7c2831e3beb0190ca Mon Sep 17 00:00:00 2001 From: EduardoGallego94 Date: Mon, 30 Dec 2024 19:17:45 +0100 Subject: [PATCH 06/10] Update content/python/concepts/dates/terms/strptime/strptime.md Co-authored-by: Mamta Wardhani --- content/python/concepts/dates/terms/strptime/strptime.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index 2aff27b2db5..659569d0083 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -28,9 +28,7 @@ datetime.strptime(date_string, format) - `date_string` (str): The string representing the date and/or time to be parsed.. - `format` (str): A string that defines the structure of `date_string` using format codes from the `datetime` module (e.g., `%Y` for a four-digit year, `%m` for a two-digit month). -### Returns: - -- A `datetime` object corresponding to the parsed date and time. +It returns a `datetime` object, which represents the parsed date and time from the provided `date_string` according to the specified `format`. ## Codebyte Example From 0f741c521f695c836eafedfea28b4239d3a7ecd1 Mon Sep 17 00:00:00 2001 From: EduardoGallego94 Date: Mon, 30 Dec 2024 19:19:32 +0100 Subject: [PATCH 07/10] Update content/python/concepts/dates/terms/strptime/strptime.md Co-authored-by: Mamta Wardhani --- content/python/concepts/dates/terms/strptime/strptime.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index 659569d0083..9309069a0ee 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -17,7 +17,7 @@ The **`.strptime()`** is a method included in the [`datetime`](https://www.codec ## Syntax -```python +```pseudo from datetime import datetime datetime.strptime(date_string, format) From 60df20b68991280dc0093fe04680f87a56d69b7c Mon Sep 17 00:00:00 2001 From: EduardoGallego94 Date: Mon, 30 Dec 2024 19:40:24 +0100 Subject: [PATCH 08/10] strptime updated --- .../concepts/dates/terms/strptime/strptime.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index 9309069a0ee..17dc59c1e1b 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -30,6 +30,21 @@ datetime.strptime(date_string, format) It returns a `datetime` object, which represents the parsed date and time from the provided `date_string` according to the specified `format`. +## Example + +```python +from datetime import datetime + +# Define the date-time string and format +datetime_string = "27/12/2024 15:30:00" +datetime_format = "%d/%m/%Y %H:%M:%S" + +# Parse the string into a datetime object +dt_object = datetime.strptime(datetime_string, datetime_format) + +print(dt_object) #Output: 2024-12-27 15:30:00 +``` + ## Codebyte Example ```codebyte/python From 78ed574813c66beee79d5e84475633ebdb6f86ea Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Wed, 8 Jan 2025 10:49:13 +0530 Subject: [PATCH 09/10] Update strptime.md minor fixes --- .../concepts/dates/terms/strptime/strptime.md | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index 17dc59c1e1b..e6ef7393f11 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -23,16 +23,16 @@ from datetime import datetime datetime.strptime(date_string, format) ``` -### Parameters: - -- `date_string` (str): The string representing the date and/or time to be parsed.. -- `format` (str): A string that defines the structure of `date_string` using format codes from the `datetime` module (e.g., `%Y` for a four-digit year, `%m` for a two-digit month). +- `date_string`: The string representing the date and/or time to be parsed.. +- `format`: A string that defines the structure of `date_string` using format codes from the `datetime` module (e.g., `%Y` for a four-digit year, `%m` for a two-digit month). It returns a `datetime` object, which represents the parsed date and time from the provided `date_string` according to the specified `format`. ## Example -```python +In the following example, the `.strptime()` method is used to parse a date-time string into a `datetime` object based on the specified format: + +```py from datetime import datetime # Define the date-time string and format @@ -42,20 +42,28 @@ datetime_format = "%d/%m/%Y %H:%M:%S" # Parse the string into a datetime object dt_object = datetime.strptime(datetime_string, datetime_format) -print(dt_object) #Output: 2024-12-27 15:30:00 +print(dt_object) +``` + +The code above produces an output as: + +```shell +2024-12-27 15:30:00 ``` ## Codebyte Example +Run the following codebyte example to understand the use of the `.strptime()` method: + ```codebyte/python from datetime import datetime -# Define the date-time string and format -datetime_string = "27/12/2024 15:30:00" -datetime_format = "%d/%m/%Y %H:%M:%S" +# Define the date string and format +date_string = "2025-01-08" +date_format = "%Y-%m-%d" # Parse the string into a datetime object -dt_object = datetime.strptime(datetime_string, datetime_format) +dt_object = datetime.strptime(date_string, date_format) -print(dt_object) # Output: 2024-12-27 15:30:00 -``` \ No newline at end of file +print(dt_object) +``` From a089202ea89a1d22f0b5e58a8c68bf61a7a47ead Mon Sep 17 00:00:00 2001 From: Sriparno Roy Date: Sun, 12 Jan 2025 13:17:41 +0530 Subject: [PATCH 10/10] Minor changes --- .../concepts/dates/terms/strptime/strptime.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/content/python/concepts/dates/terms/strptime/strptime.md b/content/python/concepts/dates/terms/strptime/strptime.md index e6ef7393f11..2cef73d78e8 100644 --- a/content/python/concepts/dates/terms/strptime/strptime.md +++ b/content/python/concepts/dates/terms/strptime/strptime.md @@ -1,16 +1,16 @@ --- -Title: '.strptime()' +Title: '.strptime()' Description: 'Returns a datetime object that represents the parsed date and time from the given string, based on the specified format.' Subjects: -- 'Python' -- 'Computer Science' + - 'Python' + - 'Computer Science' Tags: -- 'Date' -- 'Time' -- 'Strings' + - 'Date' + - 'Time' + - 'Strings' CatalogContent: -- 'learn-python-3' -- 'paths/computer-science' + - 'learn-python-3' + - 'paths/computer-science' --- The **`.strptime()`** is a method included in the [`datetime`](https://www.codecademy.com/resources/docs/python/dates) 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. @@ -23,7 +23,7 @@ from datetime import datetime datetime.strptime(date_string, format) ``` -- `date_string`: The string representing the date and/or time to be parsed.. +- `date_string`: The string representing the date and/or time to be parsed. - `format`: A string that defines the structure of `date_string` using format codes from the `datetime` module (e.g., `%Y` for a four-digit year, `%m` for a two-digit month). It returns a `datetime` object, which represents the parsed date and time from the provided `date_string` according to the specified `format`.