Random Date Format Converter

  • Share this:

Code introduction


This function accepts a date string and formats it into a random date format.


Technology Stack : Behave, datetime, random

Code Type : Behave Step Function

Code Difficulty : Intermediate


                
                    
from behave import given, when, then
from random import choice
from datetime import datetime

def random_date_format(date_str):
    """
    This function takes a date string and formats it into a random date format.
    """
    formats = ["%Y-%m-%d", "%d/%m/%Y", "%m-%d-%Y", "%Y/%m/%d"]
    formatted_date = datetime.strptime(date_str, "%Y-%m-%d").strftime(choice(formats))
    return formatted_date