Function addMonth

  • Adds a specified number of months to a given date string.

    Parameters

    • date: string

      The initial date as a string in the format 'YYYY-MM-DD'.

    • month: number = 0

      The number of months to add to the date. Defaults to 0.

    Returns string

    The new date as a string in the format 'YYYY-MM-DD'.

    const result = addMonth('2023-01-15', 2);
    console.log(result); // '2023-03-15'
    const result = addMonth('2023-11-30', 3);
    console.log(result); // '2024-02-29'