Random Date Format Conversion

  • Share this:

Code introduction


This function takes a date string in the format YYYY-MM-DD and returns it in a random format.


Technology Stack : Python's built-in random and datetime modules

Code Type : Python Function

Code Difficulty : Intermediate


                
                    
import random

def random_date_format(date):
    """
    This function takes a date in the format YYYY-MM-DD and returns it in a random format.
    """
    formats = ["%Y-%m-%d", "%d/%m/%Y", "%m-%d-%Y", "%Y/%m/%d", "%d-%m-%Y", "%m/%d/%Y"]
    return date.strftime(random.choice(formats))