chronia

truncMillisecond

Overview

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.

Signature

function truncMillisecond(date: DateInput): Date;

Parameters

Parameter Type Description
date DateInput A Date object, numeric timestamp, or ISO 8601 string to truncate

Return Value

Type Description
Date A new Date object with the same millisecond value as the input, or Invalid Date if the input is invalid

Description

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.

Specification

Returns a new Date object with the same value when:

Returns Invalid Date when:

Behavior Notes

Use Cases

Usage Examples

API Consistency

import { 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

Date Copying

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

Input Validation with Copy

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

Preserving Full Precision

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)