addMonths
A utility function to add or subtract a specified number of months to/from a given date.> addMonths(months: number, date: Date = new Date()): Date
- months (number): The number of months to add. Pass a negative number to subtract months.
- date (Date, optional): The base date to modify. Defaults to the current date (new Date()).
- Returns: A new Date object with the specified number of months added.
Example
import { addMonths } from "@explita/daily-toolset";
// Add 3 months to the current date
const futureDate = addMonths(3);
console.log(futureDate); // e.g., 2025-02-22T00:00:00.000Z
// Subtract 2 months from a specific date
const pastDate = addMonths(-2, new Date("2024-11-22"));
console.log(pastDate); // e.g., 2024-09-22T00:00:00.000Z
// Add 12 months to a specific date
const customDate = new Date("2024-01-01");
const result = addMonths(12, customDate);
console.log(result); // e.g., 2025-01-01T00:00:00.000Z
Use Cases
- Calculating dates for recurring monthly events or subscriptions.
- Determining future or past months for reports or analyses.
- Adjusting dates for calendar calculations.