Day.js
A 2 KB Moment.js replacement with a nearly identical API.
What it is
Day.js is a minimalist JavaScript library for parsing, validating, manipulating, and formatting dates. It provides a modern API similar to Moment.js but with a much smaller footprint.
Day.js offers simple functions for working with dates, including parsing, formatting, adding or subtracting time, comparing dates, and working with relative times or durations.
- Licence
- MIT
- Best known for
- Moment's API in a fraction of the size
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Migrating away from Moment with minimal code changes
- You want chainable date formatting and bundle size matters
Look elsewhere when
- You need extensive timezone or locale work without pulling in several plugins
Installation
npm install dayjsGetting started
The smallest useful thing you can do with it, and what each part means.
import dayjs from 'dayjs';
const now = dayjs();
console.log(now.format('YYYY-MM-DD HH:mm:ss'));const date = dayjs('2025-08-22');
console.log(date.format('MMMM D, YYYY'));Advanced usage
Where the library earns its place over a simpler alternative.
const future = dayjs().add(7, 'day').subtract(2, 'hour');
console.log(future.format());const d1 = dayjs('2025-08-22');
const d2 = dayjs('2025-09-01');
console.log(d1.isBefore(d2)); // true
console.log(d1.isAfter(d2)); // falseimport relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);
const past = dayjs('2025-08-15');
console.log(past.fromNow());import utc from 'dayjs/plugin/utc';
dayjs.extend(utc);
const utcTime = dayjs.utc('2025-08-22 12:00');
console.log(utcTime.format());Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Invalid Date
- Ensure the input string is in a valid format or use `dayjs(string, format)` with the correct format specified.
- Plugin not loaded
- Extend Day.js with required plugins before using their functions, e.g., `dayjs.extend(plugin)`.
Best practices
- Use plugins only for features you need to minimize bundle size.
- Prefer Day.js for modern JavaScript projects requiring small date libraries.
- Use immutable objects to avoid unintended mutations.
- Use ISO 8601 strings when possible for consistent parsing.
- Combine with timezone plugins for handling different time zones.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Day.js was created to provide a lightweight alternative to Moment.js while maintaining a familiar API. Its modular architecture allows importing only the features you need, reducing bundle size significantly for web applications.
