Day.js is a minimalist JavaScript library that parses, validates, manipulates, and displays dates and times for modern browsers with a largely Moment.js-compatible API. At only 2KB, it provides immutable date operations, chainable methods, and comprehensive internationalization support through its plugin architecture.
npm install dayjsimport dayjs from 'dayjs';For CommonJS:
const dayjs = require('dayjs');Plugin imports:
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';
dayjs.extend(utc);
dayjs.extend(timezone);import dayjs from 'dayjs';
// Create dayjs objects
const now = dayjs();
const birthday = dayjs('1990-01-25');
const timestamp = dayjs(1318781876406);
// Chainable manipulation
const nextWeek = dayjs()
.add(1, 'week')
.startOf('day')
.format('YYYY-MM-DD');
// Query operations
if (dayjs().isAfter(birthday)) {
console.log('Birthday has passed');
}
// Format display
console.log(dayjs().format('YYYY-MM-DD HH:mm:ss'));
console.log(dayjs().format('[Today is] dddd'));Day.js is built around several key architectural patterns:
Main entry point for creating Dayjs instances from various input types.
/**
* Creates a new Dayjs instance (basic overload)
* @param date - Input date (string, number, Date, Dayjs, null, undefined)
* @returns New Dayjs instance
*/
function dayjs(date?: ConfigType): Dayjs;
/**
* Creates a new Dayjs instance with format (format overload)
* @param date - Input date
* @param format - Parse format string or format object
* @param strict - Strict parsing mode
* @returns New Dayjs instance
*/
function dayjs(date?: ConfigType, format?: OptionType, strict?: boolean): Dayjs;
/**
* Creates a new Dayjs instance with format and locale (full overload)
* @param date - Input date
* @param format - Parse format string or format object
* @param locale - Locale string
* @param strict - Strict parsing mode
* @returns New Dayjs instance
*/
function dayjs(date?: ConfigType, format?: OptionType, locale?: string, strict?: boolean): Dayjs;
type ConfigType = string | number | Date | Dayjs | null | undefined;
type OptionType = FormatObject | string | string[];
interface FormatObject {
locale?: string;
format?: string;
utc?: boolean;
}Static utility methods available on the dayjs function itself.
/**
* Extend dayjs with plugin functionality
* @param plugin - Plugin function
* @param option - Plugin options
* @returns dayjs function for chaining
*/
dayjs.extend<T>(plugin: PluginFunc<T>, option?: T): typeof dayjs;
/**
* Set or get global locale
* @param preset - Locale name or locale object
* @param object - Locale definition object
* @param isLocal - Local-only setting
* @returns Current locale name or dayjs function
*/
dayjs.locale(preset?: string | ILocale, object?: Partial<ILocale>, isLocal?: boolean): string;
/**
* Check if value is a Dayjs instance
* @param d - Value to check
* @returns True if value is Dayjs instance
*/
dayjs.isDayjs(d: any): d is Dayjs;
/**
* Create Dayjs from Unix timestamp
* @param timestamp - Unix timestamp in seconds
* @returns New Dayjs instance
*/
dayjs.unix(timestamp: number): Dayjs;Extensible plugin architecture for adding functionality without increasing core bundle size.
interface PluginFunc<T = unknown> {
(option: T, dayjsClass: typeof Dayjs, dayjsFactory: typeof dayjs): void;
}
// Plugin installation
dayjs.extend(pluginFunction, options);Comprehensive locale support with over 100 built-in locales and customizable locale definitions.
// Global locale setting
dayjs.locale('es'); // Spanish globally
// Instance-specific locale
dayjs().locale('zh-cn'); // Chinese for this instance
// Locale object structure
interface ILocale {
name: string;
weekdays?: string[];
months?: string[];
weekStart?: number;
yearStart?: number;
ordinal?: (n: number) => string;
formats?: Record<string, string>;
relativeTime?: Record<string, string>;
}// Main Dayjs class interface
interface Dayjs {
// Core methods - see Core API documentation for full details
year(): number;
year(value: number): Dayjs;
month(): number;
month(value: number): Dayjs;
date(): number;
date(value: number): Dayjs;
day(): 0 | 1 | 2 | 3 | 4 | 5 | 6;
day(value: number): Dayjs;
hour(): number;
hour(value: number): Dayjs;
minute(): number;
minute(value: number): Dayjs;
second(): number;
second(value: number): Dayjs;
millisecond(): number;
millisecond(value: number): Dayjs;
set(unit: UnitType, value: number): Dayjs;
get(unit: UnitType): number;
add(value: number, unit?: ManipulateType): Dayjs;
subtract(value: number, unit?: ManipulateType): Dayjs;
startOf(unit: OpUnitType): Dayjs;
endOf(unit: OpUnitType): Dayjs;
format(template?: string): string;
diff(date?: ConfigType, unit?: QUnitType | OpUnitType, float?: boolean): number;
valueOf(): number;
unix(): number;
daysInMonth(): number;
toDate(): Date;
toJSON(): string;
toISOString(): string;
toString(): string;
utcOffset(): number;
isBefore(date?: ConfigType, unit?: OpUnitType): boolean;
isSame(date?: ConfigType, unit?: OpUnitType): boolean;
isAfter(date?: ConfigType, unit?: OpUnitType): boolean;
isValid(): boolean;
clone(): Dayjs;
locale(): string;
locale(preset: string | ILocale, object?: Partial<ILocale>): Dayjs;
}
// Type definitions for units and configuration
type ConfigType = string | number | Date | Dayjs | null | undefined;
type UnitTypeShort = 'd' | 'D' | 'M' | 'y' | 'h' | 'm' | 's' | 'ms';
type UnitTypeLong = 'millisecond' | 'second' | 'minute' | 'hour' | 'day' | 'month' | 'year' | 'date';
type UnitTypeLongPlural = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days' | 'months' | 'years' | 'dates';
type UnitType = UnitTypeLong | UnitTypeLongPlural | UnitTypeShort;
type OpUnitType = UnitType | "week" | "weeks" | 'w';
type QUnitType = UnitType | "quarter" | "quarters" | 'Q';
type ManipulateType = Exclude<OpUnitType, 'date' | 'dates'>;