The diffMilliseconds function calculates the exact difference in milliseconds between two dates, providing the most precise time difference measurement available in the Chronia library. It serves as the foundation for all time difference calculations.
function diffMilliseconds(dateLeft: DateInput, dateRight: DateInput): number;
| Parameter | Type | Description |
|---|---|---|
dateLeft |
DateInput |
The first date as a Date object, numeric timestamp, or ISO 8601 string (subtrahend in the calculation) |
dateRight |
DateInput |
The second date as a Date object, numeric timestamp, or ISO 8601 string (minuend in the calculation) |
| Type | Description |
|---|---|
number |
The difference in milliseconds. Returns a positive value if dateLeft is after dateRight, negative if before, 0 if identical, or NaN if any input is invalid |
The diffMilliseconds function computes the precise time difference between two dates at millisecond resolution. This is equivalent to calling dateLeft.getTime() - dateRight.getTime(), but with built-in validation and consistent error handling. The function is fundamental to Chronia’s time difference calculations and serves as the basis for higher-level difference functions.
dateLeft represents a later point in time than dateRightdateLeft.getTime() - dateRight.getTime()0 when:dateLeft represents an earlier point in time than dateRightNaN when:dateLeft or dateRight is an Invalid Date object (e.g., new Date('invalid'))NaNInfinity or -InfinityNaNNaN for invalid inputs, which differs from boolean functions (return false) and comparison functions (throw errors)diffSeconds, diffMinutes, etc., for larger time unitsimport { diffMilliseconds } from "chronia";
// Measure function execution time
function measurePerformance<T>(fn: () => T): { result: T; elapsed: number } {
const startTime = Date.now();
const result = fn();
const endTime = Date.now();
const elapsed = diffMilliseconds(endTime, startTime);
return { result, elapsed };
}
// Usage
const { result, elapsed } = measurePerformance(() => {
// Some expensive operation
return Array(1000000)
.fill(0)
.map((_, i) => i * 2);
});
console.log(`Operation took ${elapsed}ms`); // e.g., "Operation took 42ms"
import { diffMilliseconds } from "chronia";
// Calculate exact time difference
const start = new Date(2024, 5, 15, 14, 30, 45, 100);
const end = new Date(2024, 5, 15, 14, 30, 45, 500);
diffMilliseconds(end, start); // Returns: 400
// Same time returns 0
const date1 = new Date(2024, 5, 15, 14, 30, 45, 123);
const date2 = new Date(2024, 5, 15, 14, 30, 45, 123);
diffMilliseconds(date1, date2); // Returns: 0
// Negative result when first date is earlier
diffMilliseconds(start, end); // Returns: -400
import { diffMilliseconds } from "chronia";
// Compare numeric timestamps
const timestamp1 = 1718459446000; // Unix timestamp
const timestamp2 = 1718459445000;
diffMilliseconds(timestamp1, timestamp2); // Returns: 1000
// Mix Date objects and timestamps
const dateObj = new Date(2024, 5, 15, 14, 30, 46, 0);
const timestamp = new Date(2024, 5, 15, 14, 30, 45, 0).getTime();
diffMilliseconds(dateObj, timestamp); // Returns: 1000
diffMilliseconds(timestamp, dateObj); // Returns: -1000
import { diffMilliseconds } from "chronia";
// Invalid Date returns NaN
const invalidDate = new Date("invalid");
const validDate = new Date(2024, 5, 15);
diffMilliseconds(invalidDate, validDate); // Returns: NaN
diffMilliseconds(validDate, invalidDate); // Returns: NaN
// Check for valid result
function safeDiff(date1: Date | number, date2: Date | number): number | null {
const diff = diffMilliseconds(date1, date2);
return isNaN(diff) ? null : diff;
}
safeDiff(validDate, invalidDate); // Returns: null
safeDiff(validDate, validDate); // Returns: 0
import { diffMilliseconds } from "chronia";
// Calculate animation progress
class Animation {
private startTime: number;
private duration: number;
constructor(durationMs: number) {
this.startTime = Date.now();
this.duration = durationMs;
}
getProgress(): number {
const elapsed = diffMilliseconds(Date.now(), this.startTime);
return Math.min(elapsed / this.duration, 1); // Clamp to [0, 1]
}
isComplete(): boolean {
return this.getProgress() >= 1;
}
}
// Usage
const animation = new Animation(1000); // 1 second animation
console.log(animation.getProgress()); // e.g., 0.235 (23.5% complete)