The subDays function subtracts a specified number of days from a given date, returning a new Date object. It provides a convenient way to perform date arithmetic with automatic validation and consistent behavior across Chronia’s API.
function subDays(date: DateInput, amount: number): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
The base date as a Date object, numeric timestamp, or ISO 8601 string from which days will be subtracted |
amount |
number |
The number of days to subtract (can be negative to add days instead) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified number of days subtracted, or Invalid Date if any input is invalid |
The subDays function performs date arithmetic by subtracting a specified number of days from a given date. It validates all inputs before processing and leverages the addDays function internally by negating the amount, ensuring consistent behavior across the library’s date manipulation functions.
date argument is a valid Date objectdate argument is a finite numeric timestamp (positive, zero, or negative)amount argument is a finite number (including integers and decimals)Math.trunc (e.g., 1.9 → 1, -1.9 → -1)date argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaNdate argument is Infinity or -Infinityamount argument is NaNamount argument is Infinity or -Infinitydate and amount parameters before processingMath.trunc to truncate fractional days toward zero (1.9 becomes 1, -1.9 becomes -1)subDays(date, -3) adds 3 days)addDays(date, -amount) for consistent behaviorDate | number types are accepted for the date parameterimport { subDays } from "chronia";
// Calculate a date 5 days ago
const today = new Date(2025, 0, 10); // January 10, 2025
const fiveDaysAgo = subDays(today, 5);
// Returns: Date object representing January 5, 2025
// Works with timestamps
const timestamp = Date.now();
const oneWeekAgo = subDays(timestamp, 7);
// Returns: Date object representing 7 days ago from now
// Calculate historical date
const projectStart = new Date(2025, 5, 1); // June 1, 2025
const twoWeeksEarlier = subDays(projectStart, 14);
// Returns: Date object representing May 18, 2025
import { subDays } from "chronia";
// Create a 30-day date range ending today
const endDate = new Date(2025, 0, 31); // January 31, 2025
const startDate = subDays(endDate, 30);
// Returns: Date object representing January 1, 2025
// Filter logs from the last 7 days
function getRecentLogs(logs: Array<{ date: Date; message: string }>) {
const sevenDaysAgo = subDays(new Date(), 7);
return logs.filter((log) => log.date >= sevenDaysAgo);
}
import { subDays } from "chronia";
// Calculate remaining trial days
function calculateTrialRemaining(
startDate: Date,
trialLength: number,
elapsed: number,
): Date {
const trialEnd = subDays(startDate, -trialLength); // Negative to add
return subDays(trialEnd, elapsed);
}
const subscriptionStart = new Date(2025, 0, 1);
const remainingDate = calculateTrialRemaining(subscriptionStart, 30, 10);
// Returns: Date representing 20 days into the trial
import { subDays } from "chronia";
// Calculate payment due date (invoice date minus grace period)
function calculateDueDate(invoiceDate: Date, gracePeriod: number): Date {
return subDays(invoiceDate, -gracePeriod); // Negative to add grace period
}
const invoice = new Date(2025, 0, 15); // January 15, 2025
const dueDate = calculateDueDate(invoice, 30);
// Returns: Date object representing February 14, 2025
// Notice period calculation
function getLastWorkingDay(resignationDate: Date, noticeDays: number): Date {
return subDays(resignationDate, -noticeDays);
}
const resignation = new Date(2025, 0, 1);
const lastDay = getLastWorkingDay(resignation, 14);
// Returns: Date object representing January 15, 2025
import { subDays } from "chronia";
// Use negative amounts to add days
const baseDate = new Date(2025, 0, 10); // January 10, 2025
// Subtract days
const past = subDays(baseDate, 5);
// Returns: January 5, 2025
// Add days using negative amount
const future = subDays(baseDate, -5);
// Returns: January 15, 2025
// Fractional amounts are truncated
const truncated = subDays(baseDate, 3.9);
// Returns: January 7, 2025 (3.9 truncated to 3)
import { subDays } from "chronia";
// Invalid date input
const invalidResult = subDays(new Date("invalid"), 5);
// Returns: Invalid Date
// Invalid amount (NaN)
const nanResult = subDays(new Date(2025, 0, 1), NaN);
// Returns: Invalid Date
// Invalid amount (Infinity)
const infinityResult = subDays(new Date(2025, 0, 1), Infinity);
// Returns: Invalid Date
// Validate result before use
function safeDateSubtraction(date: Date, days: number): Date | null {
const result = subDays(date, days);
return isNaN(result.getTime()) ? null : result;
}
const safeResult = safeDateSubtraction(new Date(2025, 0, 10), 5);
// Returns: Date object representing January 5, 2025
const safeInvalid = safeDateSubtraction(new Date("invalid"), 5);
// Returns: null