Random Date Generator within a Range

  • Share this:

Code introduction


The function receives two Arrow library Arrow objects as arguments, which represent a date range. The function returns a random date within this range.


Technology Stack : Arrow library

Code Type : The type of code

Code Difficulty : Intermediate


                
                    
import random
from arrow import arrow, get

def random_date(arg1, arg2):
    """
    Generates a random date between two dates provided as arguments.

    Args:
    arg1 (arrow.Arrow): Start date for the range.
    arg2 (arrow.Arrow): End date for the range.

    Returns:
    arrow.Arrow: A random date between the two provided dates.
    """
    delta = arg2 - arg1
    random_seconds = random.randrange(delta.seconds)
    return arg1.replace(seconds=random_seconds)                
              
Tags: