Random dates in python

Python -- Posted on Nov. 28, 2019

Random dates in python

              
                import datetime
import random

def random_date(start, end):
    """
    This function will return a random datetime between two datetime
    objects.
    """
    delta = end - start
    int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
    random_second = random.randrange(int_delta)
    return start + datetime.timedelta(seconds=random_second)


end_date = datetime.datetime.now()
start_date = end_date - datetime.timedelta(days=60)
random_date(start_date, end_date)
                  
   
            

Related Posts