You can download this code by clicking the button below.
This code is now available for download.
This function takes two date strings as parameters and generates a random time interval between those dates.
Technology Stack : Pendulum
Code Type : Custom function
Code Difficulty : Intermediate
import random
from pendulum import DateTime, Interval
def random_interval(start_date, end_date):
"""
Generate a random interval between two dates.
:param start_date: The start date as a string in 'YYYY-MM-DD' format.
:param end_date: The end date as a string in 'YYYY-MM-DD' format.
:return: A random Interval object between start_date and end_date.
"""
start = DateTime(start_date)
end = DateTime(end_date)
random_seconds = random.randint(0, (end - start).in_seconds())
return Interval(seconds=random_seconds)