DEV Community

Arhan Khan
Arhan Khan

Posted on

Day.js for Beginners: Tiny Dates Without Moment.js

JavaScript's built-in Date is awkward, and Moment.js — the old answer — is now too large for modern apps at nearly 300 KB. Day.js is a 2 KB alternative with the same friendly API, and for most apps it does everything you need.

Installation is one package, and the API will feel familiar if you have used Moment:

import dayjs from 'dayjs';

const now = dayjs();
console.log(now.format('DD MMM YYYY, hh:mm A')); // 21 Nov 2026, 07:59 PM

const nextWeek = dayjs().add(7, 'day');
console.log(nextWeek.format('dddd, DD MMMM YYYY')); // Friday, 28 November 2026

const past = dayjs('2026-01-15');
console.log(dayjs().diff(past, 'month')); // months since 15 January
Enter fullscreen mode Exit fullscreen mode

Day.js objects are immutable — add returns a new object instead of changing the original, which prevents a whole class of bugs. And the library is plugin-based: the core is tiny, and you add only what you need, like relative time or timezones:

import relativeTime from 'dayjs/plugin/relativeTime';
dayjs.extend(relativeTime);

console.log(dayjs('2026-11-20').fromNow()); // a day ago
console.log(dayjs().to(dayjs().add(2, 'hour'))); // in 2 hours
Enter fullscreen mode Exit fullscreen mode

Common plugins cover relative time ("3 hours ago"), timezones, durations, custom parsing formats, and calendar display. Because the API mirrors Moment.js, migrating an old project is often a matter of changing the import.

Day.js is for JavaScript developers who need to format, parse, and compare dates without shipping a huge library. If your app shows "posted 2 hours ago", schedules reminders, or formats dates for users, Day.js is one of the smallest and simplest tools for the job.

Top comments (0)