You can download this code by clicking the button below.
This code is now available for download.
This code defines a function that generates a random datetime between two specified dates.
Technology Stack : The code uses the 'datetime' and 'random' modules from Python's built-in libraries.
Code Type : Function
Code Difficulty : Intermediate
import datetime
import random
def generate_random_datetime(start_date, end_date):
"""
Generate a random datetime between two given dates.
Args:
start_date (str): The start date in 'YYYY-MM-DD' format.
end_date (str): The end date in 'YYYY-MM-DD' format.
Returns:
datetime.datetime: A random datetime between the start and end dates.
"""
start = datetime.datetime.strptime(start_date, '%Y-%m-%d')
end = datetime.datetime.strptime(end_date, '%Y-%m-%d')
delta = end - start
random_days = random.randrange(delta.days)
random_datetime = start + datetime.timedelta(days=random_days)
return random_datetime