Okay, you can achieve this transformation using Python's built-in datetime module.
- Parse the input string: Use
datetime.strptime() to convert the string into a datetime object. You need to specify the exact format of the input string using format codes.
- Format the datetime object: Use the
strftime() method on the datetime object to format it into the desired output string.
Here's the Python code:
from datetime import datetime
def transform_date_string(input_date_str):
"""
Transforms a date string from format 'Day, DD Mon YYYY HH:MM:SS GMT'
to 'YYYY-MM-DD'.
Args:
input_date_str: The date string in the original format.
Returns:
The date string in the desired 'YYYY-MM-DD' format, or None if parsing fails.
"""
try:
# Define the format of the input string
# %a: Abbreviated weekday name (e.g., Wed)
# %d: Day of the month as a zero-padded decimal number (e.g., 20)
# %b: Abbreviated month name (e.g., Mar)
# %Y: Year with century (e.g., 2024)
# %H:%M:%S: Hour (24-hour clock), Minute, Second
# Note: strptime often handles trailing ' GMT' correctly if the preceding format matches.
input_format = "%a, %d %b %Y %H:%M:%S"
# Parse the input string into a datetime object
datetime_object = datetime.strptime(input_date_str, input_format)
# Define the desired output format
# %Y: Year with century
# %m: Month as a zero-padded decimal number
# %d: Day of the month as a zero-padded decimal number
output_format = "%Y-%m-%d"
# Format the datetime object into the output string
output_date_str = datetime_object.strftime(output_format)
return output_date_str
except ValueError as e:
print(f"Error parsing date string '{input_date_str}': {e}")
# Handle cases where the input string might not exactly match the format,
# perhaps the 'GMT' part causes an issue in some environments.
# You could add more robust error handling or cleaning here if needed.
return None
# --- Example Usage ---
input_string = "Wed, 20 Mar 2024 18:36:39 GMT"
output_string = transform_date_string(input_string)
if output_string:
print(f"Original: {input_string}")
print(f"Transformed: {output_string}")
# --- Another Example ---
input_string_2 = "Mon, 01 Jan 2023 10:05:00 GMT"
output_string_2 = transform_date_string(input_string_2)
if output_string_2:
print(f"Original: {input_string_2}")
print(f"Transformed: {output_string_2}")
Explanation of Format Codes:
%a: Abbreviated weekday name (e.g., 'Wed', 'Mon').
%d: Day of the month as a zero-padded decimal number (e.g., '20', '01').
%b: Abbreviated month name (e.g., 'Mar', 'Jan').
%Y: Year with century (e.g., '2024', '2023').
%H: Hour (24-hour clock) as a zero-padded decimal number (e.g., '18', '10').
%M: Minute as a zero-padded decimal number (e.g., '36', '05').
%S: Second as a zero-padded decimal number (e.g., '39', '00').
%m: Month as a zero-padded decimal number (used in output, e.g., '03', '01').
The code parses the input string using the specific format it's in (%a, %d %b %Y %H:%M:%S) and then reformats that datetime object into the desired YYYY-MM-DD format (%Y-%m-%d).