You can download this code by clicking the button below.
This code is now available for download.
This function takes a DateTime object from the Pendulum library and a format string, then returns a new DateTime string with a different format. The function randomly selects a different format each time it is called.
Technology Stack : Pendulum
Code Type : Pendulum DateTime Formatting
Code Difficulty : Intermediate
import random
from pendulum import DateTime
def random_datetime_formatting(input_datetime, output_format):
"""
This function takes a datetime object and a format string and returns a new datetime string
with a different format. The function randomly selects a different format each time it is called.
"""
formats = ["YYYY-MM-DD", "MM/DD/YYYY", "DD-MM-YYYY", "YYYY/MM/DD", "YYYY-MM-DD HH:mm:ss"]
random_format = random.choice(formats)
return input_datetime.format(random_format)