Day.js: A Tiny, Immutable Alternative to Moment.js
Working with dates in JavaScript can be messy. For years, Moment.js was the go-to solution, but its size (over 60kB) and mutability issues made developers look for lighter alternatives. Enter Day.js—a 2kB immutable date-time library that keeps the same familiar API as Moment.js but without the bloat.
What It Does
Day.js is a minimalist date library that handles parsing, manipulating, and formatting dates, just like Moment.js. The key difference? It’s immutable (operations return new instances instead of modifying the original) and tiny (2kB vs. Moment’s 60kB+). It supports:
- Chainable API (e.g.,
dayjs().add(1, 'day').format()) - Plugins for extra functionality (time zones, locales, etc.)
- Full TypeScript support
Why It’s Cool
- Same API, Zero Bloat – If you’ve used Moment.js, you already know how to use Day.js. Migrating is often just a find-and-replace.
- Plugins for Flexibility – Need timezone support? There’s a plugin for that. Custom locales? Also a plugin. The core stays lean while letting you opt into extras.
- Performance – Smaller bundle size means faster load times, especially critical for frontend apps.
How to Try It
Install via npm/yarn:
npm install dayjs
Basic usage:
import dayjs from 'dayjs';
const today = dayjs();
console.log(today.format('YYYY-MM-DD')); // Outputs current date
Check out the docs for more examples or the GitHub repo to dive deeper.
Final Thoughts
Day.js is a no-brainer if you’re still using Moment.js or need a lightweight date utility. It’s not just a "Moment.js lite"—it’s a thoughtful, modern alternative that respects bundle size and immutability. For most projects, it’s more than enough.
What’s your go-to date library? Let us know—@githubprojects.