The truncMillisecond function truncates a date to the millisecond unit. Since millisecond is the smallest unit supported by JavaScript Date objects, this function returns the same date value without any truncation, but is provided for API consistency with other truncation functions in the Chronia library.
function truncMillisecond(date: DateInput): Date;
| Parameter | Type | Description |
|---|---|---|
date |
DateInput |
A Date object, numeric timestamp, or ISO 8601 string to truncate |
| Type | Description |
|---|---|
Date |
A new Date object with the same millisecond value as the input, or Invalid Date if the input is invalid |
The truncMillisecond function creates a new Date object with the same value as the input date. While no actual truncation occurs (since millisecond is the smallest unit in JavaScript Date objects), this function maintains API consistency with other truncation functions like truncSecond, truncMinute, and truncHour.
Date object (not Invalid Date)0, representing January 1, 1970, 00:00:00.000 UTC)new Date('invalid'))NaNInfinity-InfinityDate | numberimport { truncMillisecond, truncSecond, truncMinute } from "chronia";
// Programmatically select truncation unit
type TruncUnit = "millisecond" | "second" | "minute";
function truncateToUnit(date: Date, unit: TruncUnit): Date {
switch (unit) {
case "millisecond":
return truncMillisecond(date);
case "second":
return truncSecond(date);
case "minute":
return truncMinute(date);
}
}
const date = new Date(2024, 5, 15, 14, 30, 45, 123);
truncateToUnit(date, "millisecond"); // Returns: June 15, 2024 14:30:45.123
import { truncMillisecond } from "chronia";
// Create a new Date instance with validation
const original = new Date(2024, 11, 31, 23, 59, 59, 999);
const copy = truncMillisecond(original);
// Mutating copy does not affect original
copy.setFullYear(2025);
console.log(original.getFullYear()); // Returns: 2024
console.log(copy.getFullYear()); // Returns: 2025
import { truncMillisecond } from "chronia";
// Validate and create Date instance from timestamp
function createValidDate(input: Date | number): Date | null {
const result = truncMillisecond(input);
return isNaN(result.getTime()) ? null : result;
}
// Valid timestamp
createValidDate(1704067200000); // Returns: Date object for Jan 1, 2024
createValidDate(0); // Returns: Date object for Jan 1, 1970
// Invalid inputs
createValidDate(NaN); // Returns: null
createValidDate(Infinity); // Returns: null
createValidDate(new Date("invalid")); // Returns: null
import { truncMillisecond } from "chronia";
// No truncation occurs - millisecond precision preserved
const precise = new Date(2024, 5, 15, 14, 30, 45, 123);
const result = truncMillisecond(precise);
console.log(result.getMilliseconds()); // Returns: 123 (unchanged)
// Works with timestamps
const timestamp = Date.now(); // e.g., 1704067200123
const fromTimestamp = truncMillisecond(timestamp);
console.log(fromTimestamp.getTime()); // Returns: 1704067200123 (same value)