The addMilliseconds function adds a specified number of milliseconds to a given date, returning a new Date object. It provides a reliable way to perform millisecond-level date arithmetic in Chronia’s consistent API surface.
function addMilliseconds(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 milliseconds to add (can be negative to subtract) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified milliseconds added, or Invalid Date if any input is invalid |
The addMilliseconds function performs precise millisecond-level arithmetic on dates. It validates all inputs before processing and returns a new Date instance without mutating the original date. Fractional milliseconds in the amount parameter are truncated toward zero using Math.trunc.
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)amount argument is a finite number, including:
0, returns equivalent date)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 -InfinityisValidDateOrNumber, isValidNumber) for consistencyMath.trunc before additionDate | number for date and number for amountimport { addMilliseconds } from "chronia";
// Add positive milliseconds
const baseDate = new Date(2020, 0, 1, 12, 0, 0, 0);
const result = addMilliseconds(baseDate, 500);
// Returns: Date representing 2020-01-01T12:00:00.500
// Subtract milliseconds using negative amount
const withMillis = new Date(2020, 0, 1, 12, 0, 0, 500);
const subtracted = addMilliseconds(withMillis, -300);
// Returns: Date representing 2020-01-01T12:00:00.200
// Fractional amounts are truncated toward zero
const fractional = addMilliseconds(new Date(2020, 0, 1), 1.9);
// Returns: Date with 1 millisecond added (1.9 truncated to 1)
const negativeFractional = addMilliseconds(new Date(2020, 0, 1), -1.9);
// Returns: Date with 1 millisecond subtracted (-1.9 truncated to -1)
import { addMilliseconds } from "chronia";
// Apply network latency compensation
function compensateForLatency(timestamp: number, latencyMs: number): Date {
return addMilliseconds(timestamp, latencyMs);
}
const serverTime = Date.now();
const adjustedTime = compensateForLatency(serverTime, 150);
// Returns: Date adjusted by 150ms latency
// Synchronize timestamps across systems
const receivedTimestamp = 1704110400000;
const synchronized = addMilliseconds(receivedTimestamp, -50);
// Returns: Date adjusted backward by 50ms
import { addMilliseconds } from "chronia";
// Calculate frame timestamps for 60fps animation
function calculateFrameTimestamps(startDate: Date, frameCount: number): Date[] {
const frameIntervalMs = 1000 / 60; // ~16.67ms per frame
const frames: Date[] = [];
for (let i = 0; i < frameCount; i++) {
frames.push(addMilliseconds(startDate, frameIntervalMs * i));
}
return frames;
}
const animationStart = new Date(2025, 0, 1, 12, 0, 0, 0);
const frameTimestamps = calculateFrameTimestamps(animationStart, 10);
// Returns: Array of 10 Date objects at ~16.67ms intervals
import { addMilliseconds } from "chronia";
// Crossing second boundary
const nearBoundary = new Date(2020, 0, 1, 12, 0, 0, 999);
const crossedSecond = addMilliseconds(nearBoundary, 1);
// Returns: Date representing 2020-01-01T12:00:01.000
// Crossing minute boundary
const nearMinute = new Date(2020, 0, 1, 12, 0, 59, 999);
const crossedMinute = addMilliseconds(nearMinute, 1);
// Returns: Date representing 2020-01-01T12:01:00.000
import { addMilliseconds } from "chronia";
// Invalid Date input
const invalidDate = addMilliseconds(new Date("invalid"), 500);
// Returns: Invalid Date
// NaN amount
const nanResult = addMilliseconds(new Date(), NaN);
// Returns: Invalid Date
// Infinity amount
const infinityResult = addMilliseconds(new Date(), Infinity);
// Returns: Invalid Date
// Check validity before use
function safeAddMilliseconds(date: Date, amount: number): Date | null {
const result = addMilliseconds(date, amount);
return isNaN(result.getTime()) ? null : result;
}