Calculate Days Between Dates in YYYY-MM-DD Format

  • Share this:

Code introduction


This function takes two date strings as input in the format YYYY-MM-DD, and calculates the number of days between the two dates.


Technology Stack : datetime

Code Type : Date calculation function

Code Difficulty : Intermediate


                
                    
import datetime

def calculate_days_between_dates(start_date, end_date):
    """
    Calculate the number of days between two dates.
    """
    start = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end = datetime.datetime.strptime(end_date, '%Y-%m-%d')
    delta = end - start
    return delta.days                
              
Tags: