chronia

min

Overview

The min function returns the earliest (minimum) date from a given set of dates or timestamps. It provides a simple and reliable way to find the minimum value among multiple date values in Chronia’s consistent API surface.

Signature

function min(...dates: DateInput[]): Date;

Parameters

Parameter Type Description
...dates DateInput[] One or more Date objects, numeric timestamps, or ISO 8601 strings to compare. Accepts any number of arguments (variadic).

Return Value

Type Description
Date Returns a new Date object representing the earliest date among the inputs. Returns Invalid Date (new Date(NaN)) if any input is invalid or if no arguments are provided.

Description

The min function determines which date among the provided arguments represents the earliest point in time. It accepts a flexible combination of Date objects and numeric timestamps, making it versatile for various date comparison scenarios.

Specification

Returns a valid Date representing the earliest date when:

Returns Invalid Date (new Date(NaN)) when:

Behavior Notes

Use Cases

Usage Examples

Finding Earliest Events

import { min } from "chronia";

// Compare multiple event dates
const event1 = new Date(2024, 5, 15); // June 15, 2024
const event2 = new Date(2024, 5, 10); // June 10, 2024
const event3 = new Date(2024, 5, 20); // June 20, 2024
const earliestEvent = min(event1, event2, event3);
// Returns: Date object for June 10, 2024

// Mixed Date objects and timestamps
const eventDate = new Date(2024, 5, 15);
const eventTimestamp = 1718409600000; // June 15, 2024, 00:00:00 UTC
const earliest = min(eventDate, eventTimestamp, Date.now());
// Returns: The earliest among the three dates

Date Range Validation

import { min } from "chronia";

// Find the earliest start date for a merged range
function mergeRanges(range1: { start: Date }, range2: { start: Date }) {
  return {
    start: min(range1.start, range2.start),
  };
}

const range1 = { start: new Date(2024, 0, 1) };
const range2 = { start: new Date(2024, 0, 15) };
const merged = mergeRanges(range1, range2);
// Returns: { start: Date object for Jan 1, 2024 }

Historical Data Analysis

import { min } from "chronia";

// Find the oldest record in a dataset
interface Record {
  id: string;
  createdAt: Date;
}

function findOldestRecord(records: Record[]): Date | null {
  if (records.length === 0) return null;

  const dates = records.map((r) => r.createdAt);
  return min(...dates);
}

const records = [
  { id: "1", createdAt: new Date(2024, 0, 15) },
  { id: "2", createdAt: new Date(2024, 0, 5) },
  { id: "3", createdAt: new Date(2024, 0, 10) },
];
const oldest = findOldestRecord(records);
// Returns: Date object for Jan 5, 2024

Deadline Management

import { min } from "chronia";

// Find the most urgent deadline
interface Task {
  name: string;
  dueDate: Date;
}

function getNextDeadline(tasks: Task[]): Date | null {
  if (tasks.length === 0) return null;

  const dueDates = tasks.map((t) => t.dueDate);
  return min(...dueDates);
}

const tasks = [
  { name: "Project A", dueDate: new Date(2024, 6, 1) },
  { name: "Project B", dueDate: new Date(2024, 5, 25) },
  { name: "Project C", dueDate: new Date(2024, 6, 15) },
];
const nextDeadline = getNextDeadline(tasks);
// Returns: Date object for June 25, 2024

Data Aggregation

import { min } from "chronia";

// Aggregate minimum dates from multiple sources
const serverLogs = [
  { timestamp: 1718409600000 },
  { timestamp: 1718496000000 },
  { timestamp: 1718323200000 },
];

const timestamps = serverLogs.map((log) => log.timestamp);
const earliestLog = min(...timestamps);
// Returns: Date object for the earliest timestamp

// Handle invalid dates gracefully
const validDate = new Date(2024, 5, 15);
const invalidDate = new Date("invalid");
const result = min(validDate, invalidDate);
// Returns: Invalid Date (new Date(NaN))

// Single date comparison
const singleDate = new Date(2024, 5, 15);
const result2 = min(singleDate);
// Returns: New Date object for June 15, 2024

// Empty call
const empty = min();
// Returns: Invalid Date (new Date(NaN))