You can download this code by clicking the button below.
This code is now available for download.
This function takes a date string and a format string as arguments and formats the date string to the specified format. If the date string does not match the specified format, it returns an error message.
Technology Stack : datetime
Code Type : Date formatting function
Code Difficulty : Intermediate
def format_date(date_str, format_str):
from datetime import datetime
try:
formatted_date = datetime.strptime(date_str, format_str).strftime(format_str)
return formatted_date
except ValueError as e:
return str(e)