Random Period Comparison between Dates

  • Share this:

Code introduction


This function takes two date strings as arguments, converts them into Delorean objects using the Delorean library, calculates a random period between these dates, and returns it as a string.


Technology Stack : Delorean

Code Type : Function

Code Difficulty : Intermediate


                
                    
from random import choice
from delorean import Delorean, Period

def random_period_comparison(start_date, end_date):
    """
    Compares two dates and returns a random period string representation between them.
    """
    start = Delorean(start_date)
    end = Delorean(end_date)
    period = Period(start, end)
    
    comparison_type = choice(['years', 'months', 'days', 'hours', 'minutes', 'seconds'])
    result = period.get(comparison_type)
    
    return result                
              
Tags: