The addDays function adds a specified number of days to a given date and returns a new Date object. It provides a safe and validated way to perform day-based date arithmetic in Chronia’s consistent API surface.
function addDays(date: DateInput, amount: number): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
The base date as a Date object, numeric timestamp, or ISO 8601 string |
amount |
number |
The number of days to add (can be negative to subtract) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified number of days added, or Invalid Date if any input is invalid |
The addDays function performs day-based date arithmetic by adding (or subtracting when negative) a specified number of days to a base date. It validates all inputs before processing and always returns a new Date instance without mutating the original date.
Date when:date argument is a valid Date object (not Invalid Date)date argument is a finite numeric timestamp, including:
0, representing January 1, 1970, 00:00:00 UTC)date argument is a valid ISO 8601 string (e.g., "2025-01-15", "2025-01-15T14:30:00Z")amount argument is a finite number (positive, negative, or zero)amount values are automatically truncated toward zero:
1.9 becomes 1-1.9 becomes -10.5 becomes 0date argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaN, Infinity, or -Infinitydate argument is an invalid string (e.g., "not-a-date")amount argument is NaN, Infinity, or -InfinityMath.trunc(), which rounds toward zero (not floor or ceiling)Date | number types are accepted for the date parameterimport { addDays } from "chronia";
// Schedule an event 7 days from today
const today = new Date(2025, 0, 15); // January 15, 2025
const eventDate = addDays(today, 7);
// Returns: Date object for January 22, 2025
// Works with timestamps
const timestamp = Date.UTC(2025, 0, 1);
const futureDate = addDays(timestamp, 30);
// Returns: Date object for January 31, 2025
// Works with ISO 8601 strings
const fromString = addDays("2025-01-15", 7);
// Returns: Date object for January 22, 2025
const fromISOString = addDays("2025-01-01T00:00:00Z", 10);
// Returns: Date object for January 11, 2025
// Handles month overflow automatically
const endOfMonth = new Date(2025, 0, 28); // January 28
const nextMonth = addDays(endOfMonth, 5);
// Returns: Date object for February 2, 2025
import { addDays } from "chronia";
// Calculate a date 10 days ago
const today = new Date(2025, 0, 15); // January 15, 2025
const pastDate = addDays(today, -10);
// Returns: Date object for January 5, 2025
// Look back across month boundary
const monthStart = new Date(2025, 1, 3); // February 3, 2025
const previousMonth = addDays(monthStart, -5);
// Returns: Date object for January 29, 2025
import { addDays } from "chronia";
// Generate a 7-day range
function generateWeekRange(startDate: Date): Date[] {
const range: Date[] = [];
for (let i = 0; i < 7; i++) {
range.push(addDays(startDate, i));
}
return range;
}
const weekStart = new Date(2025, 0, 1);
const week = generateWeekRange(weekStart);
// Returns: Array of 7 Date objects from January 1-7, 2025
import { addDays } from "chronia";
// Fractional amounts are truncated toward zero
const baseDate = new Date(2025, 0, 1);
const result1 = addDays(baseDate, 1.9);
// Returns: January 2, 2025 (1.9 truncated to 1)
const result2 = addDays(baseDate, -1.9);
// Returns: December 31, 2024 (-1.9 truncated to -1)
const result3 = addDays(baseDate, 0.5);
// Returns: January 1, 2025 (0.5 truncated to 0, no change)
import { addDays } from "chronia";
// Invalid date input
const invalidDate = new Date("invalid");
const result1 = addDays(invalidDate, 5);
// Returns: Invalid Date
// Invalid amount input
const validDate = new Date(2025, 0, 1);
const result2 = addDays(validDate, NaN);
// Returns: Invalid Date
const result3 = addDays(validDate, Infinity);
// Returns: Invalid Date
// Check validity before using
function safeDateAdd(date: Date, days: number): Date | null {
const result = addDays(date, days);
return isNaN(result.getTime()) ? null : result;
}