Welcome to the Chronia documentation! This comprehensive guide covers everything you need to know about using and contributing to Chronia, a modern TypeScript date/time utility library.
Chronia is a modern, lightweight TypeScript date/time utility library with comprehensive formatting, parsing, and manipulation capabilities. It provides:
Date objects, timestamps, and ISO 8601 stringsChronia requires Node.js v18 to v24.
# Using pnpm (recommended)
pnpm add chronia
# Using npm
npm install chronia
# Using yarn
yarn add chronia
import { format, addDays, isBefore } from "chronia";
// Format dates
const today = new Date();
format(today, "yyyy-MM-dd"); // '2025-01-23'
// Date arithmetic
const nextWeek = addDays(today, 7);
// Date comparison
isBefore(today, nextWeek); // true
Chronia functions accept Date objects, numeric timestamps, and ISO 8601 strings:
import { isValid, getYear, addDays } from "chronia";
// Date object
isValid(new Date(2025, 0, 1)); // true
// Timestamp
isValid(1704067200000); // true
getYear(1704067200000); // 2025
// ISO 8601 string
isValid("2025-01-01"); // true
addDays("2025-01-01", 7); // January 8, 2025
getYear("2025-01-15T14:30:00Z"); // 2025
Chronia never throws exceptions. Invalid inputs return predictable values:
import { isValid, getYear, addDays } from "chronia";
// Validation functions return false
isValid(new Date("invalid")); // false
isValid(NaN); // false
// Accessor functions return NaN
getYear(NaN); // NaN
// Transformation functions return Invalid Date
const result = addDays(NaN, 5);
isValid(result); // false
All Chronia functions accept DateInput (Date | number | string) for date parameters:
type DateInput = Date | number | string;
function isValid(date: DateInput): boolean;
function format(date: DateInput, pattern: string): string;
function addDays(date: DateInput, amount: number): Date;
This provides flexibility while maintaining type safety. ISO 8601 strings are automatically parsed:
// All these are equivalent
addDays(new Date(2025, 0, 1), 7);
addDays(1735689600000, 7);
addDays("2025-01-01", 7);
addDays("2025-01-01T00:00:00Z", 7);
Transformation functions always return new Date objects:
const original = new Date(2025, 0, 1);
const modified = addDays(original, 5);
original.getTime() !== modified.getTime(); // true - original unchanged
Functions with configurable behavior use an options parameter:
import { isBefore } from "chronia";
const morning = new Date(2025, 0, 1, 9, 0);
const evening = new Date(2025, 0, 1, 17, 0);
// Default: millisecond precision
isBefore(morning, evening); // true
// Custom: day precision
isBefore(morning, evening, { unit: "day" }); // false (same day)
Chronia organizes its 76 functions into 11 logical categories:
Validate dates and compare date values
isValid, isBefore, isAfter, isEqual, isSameDay, etc.Extract components from dates
getYear, getMonth, getDay, getHours, etc.Add, subtract, and calculate date differences
addYears, addMonths, addDays, subHours, diffDays, etc.Get start/end of time periods
startOfDay, endOfMonth, startOfYear, etc.Compare and sort dates
max, min, compare, clampConvert dates to strings
format with customizable patternscreateFormatter for pre-compiled efficient formattingConvert strings to dates
parse with pattern matchingcreateParser for pre-compiled efficient parsingSet date components (immutably)
setYear, setMonth, setDay, setHours, etc.Truncate dates to specific units
truncToYear, truncToMonth, truncToDay, etc.Utility functions
now - Get current timestampUtility functions for date comparison
max, min, compare, clampimport { isValid } from "chronia";
function processDate(input: string): Date | null {
const date = new Date(input);
return isValid(date) ? date : null;
}
import { format } from "chronia";
const date = new Date(2025, 0, 23);
format(date, "yyyy-MM-dd"); // '2025-01-23'
format(date, "MMMM d, yyyy"); // 'January 23, 2025'
format(date, "MMM d, yyyy HH:mm"); // 'Jan 23, 2025 00:00'
import { addDays, addMonths, diffDays } from "chronia";
const today = new Date(2025, 0, 23);
const nextWeek = addDays(today, 7);
const nextMonth = addMonths(today, 1);
diffDays(today, nextWeek); // 7
import { isBefore, isAfter, isSameDay } from "chronia";
const date1 = new Date(2025, 0, 1);
const date2 = new Date(2025, 0, 15);
isBefore(date1, date2); // true
isAfter(date1, date2); // false
isSameDay(date1, date2); // false
import { startOfMonth, endOfMonth } from "chronia";
const date = new Date(2025, 0, 15);
const monthStart = startOfMonth(date); // 2025-01-01 00:00:00
const monthEnd = endOfMonth(date); // 2025-01-31 23:59:59
import { compare, max, min } from "chronia";
const dates = [
new Date(2025, 0, 15),
new Date(2025, 0, 1),
new Date(2025, 0, 30),
];
dates.sort(compare); // Sort chronologically
const latest = max(dates); // 2025-01-30
const earliest = min(dates); // 2025-01-01
Chronia supports localization for formatting and parsing with 39 built-in locales:
import { format } from "chronia";
import { enUS } from "chronia/locale/en-US";
import { ja } from "chronia/locale/ja";
import { fr } from "chronia/locale/fr";
const date = new Date(2025, 0, 23);
// English (default)
format(date, "MMMM d, yyyy", enUS); // 'January 23, 2025'
// Japanese
format(date, "yyyy'ๅนด'M'ๆ'd'ๆฅ'", ja); // '2025ๅนด1ๆ23ๆฅ'
// French
format(date, "EEEE d MMMM yyyy", fr); // 'jeudi 23 janvier 2025'
Core:
en-US - English (United States)ja - JapaneseWorld Languages:
zh-CN - Chinese (Simplified)ko - Koreanes - Spanishfr - Frenchde - Germanpt-BR - Portuguese (Brazil)ru - Russianar - ArabicEuropean Languages:
it - Italiannl - Dutchpl - Polishpt - Portuguese (Portugal)sv - Swedishtr - TurkishAsian Languages:
zh-TW - Chinese (Traditional, Taiwan)zh-HK - Chinese (Traditional, Hong Kong)vi - Vietnameseid - Indonesianms - Malayth - Thaihi - HindiEnglish Variants:
en-GB - English (United Kingdom)en-AU - English (Australia)en-CA - English (Canada)Major European Languages:
da - Danishfi - Finnishnb - Norwegian (Bokmรฅl)cs - Czechel - Greekhe - Hebrewhu - Hungarianro - Romanianuk - Ukrainiansk - Slovakbg - Bulgarianhr - Croatiansr - SerbianTimestamps are fastest, followed by Date objects, then strings:
// Fastest - direct timestamp
isValid(1704067200000);
// Fast - Date object
isValid(new Date(2025, 0, 1));
// Slower - requires string parsing
isValid("2025-01-01");
Millisecond comparisons are fastest:
// Fastest - millisecond comparison
isBefore(date1, date2);
// Slower - requires truncation
isBefore(date1, date2, { unit: "day" });
Cache converted values when possible:
// Less efficient
for (const event of events) {
if (isBefore(event.date, new Date())) {
// Process past event
}
}
// More efficient
const now = new Date();
for (const event of events) {
if (isBefore(event.date, now)) {
// Process past event
}
}
// Flexible date input type - accepts Date objects, timestamps, or ISO 8601 strings
type DateInput = Date | number | string;
type TimeUnit =
| "year"
| "month"
| "day"
| "hour"
| "minute"
| "second"
| "millisecond";
// Locale is a data structure containing localized strings
interface Locale {
era: { narrow: string[]; abbr: string[]; wide: string[] };
month: { narrow: string[]; abbr: string[]; wide: string[] };
weekday: { narrow: string[]; abbr: string[]; wide: string[] };
dayPeriod: { narrow: string[]; abbr: string[]; wide: string[] };
}
interface ComparisonOptions {
unit?: TimeUnit;
}
Chronia supports standard ISO 8601 date/time formats:
| Format | Example |
|---|---|
| Date only | 2025-01-15 |
| Date and time | 2025-01-15T14:30:00 |
| With milliseconds | 2025-01-15T14:30:00.000 |
| UTC (Z suffix) | 2025-01-15T14:30:00Z |
| With timezone offset | 2025-01-15T14:30:00+09:00 |
| Year and month only | 2025-01 |
All date functions follow consistent patterns:
// Validation functions return boolean
function isValid(date: DateInput): boolean;
// Accessor functions return number (or NaN for invalid input)
function getYear(date: DateInput): number;
// Transformation functions return Date
function addDays(date: DateInput, amount: number): Date;
// Comparison functions return boolean
function isBefore(
a: DateInput,
b: DateInput,
options?: ComparisonOptions,
): boolean;
Chronia follows a no-exceptions philosophy:
false for invalid inputNaN for invalid inputInvalid Date for invalid inputimport { isValid, getYear, addDays } from "chronia";
// Always validate before processing
const date = new Date(userInput);
if (!isValid(date)) {
console.error("Invalid date provided");
return;
}
// Safe to use
const year = getYear(date);
const nextWeek = addDays(date, 7);
// date-fns
import { format, addDays, isBefore } from "date-fns";
// Chronia (same API)
import { format, addDays, isBefore } from "chronia";
// moment.js
moment(date).format("YYYY-MM-DD");
moment(date).add(7, "days");
// Chronia
format(date, "yyyy-MM-dd");
addDays(date, 7);
// Day.js
dayjs(date).format("YYYY-MM-DD");
dayjs(date).add(7, "day");
// Chronia
format(date, "yyyy-MM-dd");
addDays(date, 7);
Chronia is MIT licensed.
Chronia is inspired by excellent date libraries like date-fns, moment.js, and Day.js. We thank the JavaScript community for their continued innovation in date/time handling.
Happy coding with Chronia! ๐