Random Date Time Generator Between Two Dates

  • Share this:

Code introduction


This function accepts start and end dates and then randomly generates a datetime between these two dates.


Technology Stack : Pendulum datetime, random

Code Type : Function

Code Difficulty : Intermediate


                
                    
def pendulum_random_time_period(start_date, end_date):
    from pendulum import datetime
    import random
    
    start = datetime.parse(start_date)
    end = datetime.parse(end_date)
    delta = end - start
    random_seconds = random.randrange(delta.total_seconds())
    random_time = start.add_seconds(random_seconds)
    return random_time