How to add N weeks to a given date in Python

1 Answer

0 votes
from datetime import date, timedelta

def add_weeks_to_date(base_date: date, weeks: int) -> date:
    """
    Adds a specified number of weeks to a given date.
    
    :param base_date: The original date (datetime.date instance).
    :param weeks: The number of weeks to add (can also be negative to subtract).
    :return: A new datetime.date instance with the added weeks.
    """
    # datetime.timedelta natively supports weeks. Adding it to a date object 
    # automatically manages leap years, month-end boundaries, and year rollovers.
    return base_date + timedelta(weeks=weeks)

if __name__ == "__main__":
    # Initialize a sample date: November 8, 2026
    target_date = date(2026, 11, 8)
    weeks_to_add = 4

    # Perform the addition
    result_date = add_weeks_to_date(target_date, weeks_to_add)

    # Display the formatted results using ISO format (YYYY-MM-DD)
    print(f"Original date:        {target_date.isoformat()}")
    print(f"After adding {weeks_to_add} weeks: {result_date.isoformat()}")


'''
run:

Original date:        2026-11-08
After adding 4 weeks: 2026-12-06

'''

 



answered 19 hours ago by avibootz
...