The subMilliseconds function subtracts a specified number of milliseconds from a given date. It provides a reliable way to perform millisecond-level date arithmetic while maintaining Chronia’s consistent validation and immutability patterns.
function subMilliseconds(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 to subtract milliseconds |
amount |
number |
The number of milliseconds to subtract (can be negative to add milliseconds) |
| Type | Description |
|---|---|
Date |
A new Date object with the specified number of milliseconds subtracted, or Invalid Date if any input is invalid |
The subMilliseconds function decreases the time value of the provided date by a specified number of milliseconds. It leverages Chronia’s internal addMilliseconds function by negating the amount, ensuring consistent behavior across the library. Fractional milliseconds are truncated toward zero using Math.trunc.
date argument is a valid Date object or finite numeric timestampamount argument is a finite number (including zero)1.9) are truncated to integers (1) before subtractiondate argument is an Invalid Date object (e.g., new Date('invalid'))date argument is NaN, Infinity, or -Infinityamount argument is NaN, Infinity, or -InfinityMath.trunc (e.g., 1.9 → 1, -1.9 → -1)addMilliseconds(date, -amount) for implementation consistencyDate | number for date and number for amountimport { subMilliseconds } from "chronia";
// Subtract 300 milliseconds
const date = new Date(2020, 0, 1, 12, 0, 0, 500);
const result = subMilliseconds(date, 300);
// Returns: new Date(2020, 0, 1, 12, 0, 0, 200)
// ISO: 2020-01-01T12:00:00.200
// Crossing second boundary backward
const date2 = new Date(2020, 0, 1, 12, 0, 1, 0);
const result2 = subMilliseconds(date2, 1);
// Returns: new Date(2020, 0, 1, 12, 0, 0, 999)
// ISO: 2020-01-01T12:00:00.999
import { subMilliseconds } from "chronia";
// Calculate when an event started 2500ms ago
const now = new Date(2025, 10, 22, 14, 30, 5, 750);
const eventStart = subMilliseconds(now, 2500);
// Returns: new Date(2025, 10, 22, 14, 30, 3, 250)
// 2.5 seconds earlier
// Working with timestamps
const timestamp = Date.now();
const earlier = subMilliseconds(timestamp, 1000);
// Returns: Date object 1000ms (1 second) earlier
import { subMilliseconds } from "chronia";
// Subtract a negative amount (effectively adds)
const date = new Date(2020, 0, 1, 12, 0, 0, 200);
const result = subMilliseconds(date, -300);
// Returns: new Date(2020, 0, 1, 12, 0, 0, 500)
// ISO: 2020-01-01T12:00:00.500
import { subMilliseconds } from "chronia";
// Fractional amounts are truncated toward zero
const date = new Date(2020, 0, 1, 12, 0, 0, 100);
const result = subMilliseconds(date, 1.9);
// Returns: new Date(2020, 0, 1, 12, 0, 0, 99)
// 1.9 truncated to 1, so 100 - 1 = 99
// Negative fractions also truncated
const result2 = subMilliseconds(date, -1.9);
// Returns: new Date(2020, 0, 1, 12, 0, 0, 101)
// -1.9 truncated to -1, so 100 - (-1) = 101
import { subMilliseconds } from "chronia";
// Invalid date returns Invalid Date
const invalid = subMilliseconds(new Date("invalid"), 500);
// Returns: Invalid Date
// Invalid amount returns Invalid Date
const result = subMilliseconds(new Date(), NaN);
// Returns: Invalid Date
// Infinity returns Invalid Date
const result2 = subMilliseconds(new Date(), Infinity);
// Returns: Invalid Date
// Original date is not mutated
const original = new Date(2020, 0, 1, 12, 0, 0, 500);
const modified = subMilliseconds(original, 200);
console.log(original.getMilliseconds()); // Still 500
console.log(modified.getMilliseconds()); // 300