Working with Dates in JavaScript (Node.js): Overview of Popular Libraries
Working with dates and time is one of the common tasks in web development. The built-in Date object in JavaScript has limited functionality and an inconvenient API. Fortunately, for Node.js there are many libraries that simplify parsing, formatting, manipulation and comparison of dates. In this article, we will look at four most popular libraries: Moment.js, Day.js, date-fns and Luxon.
1. Moment.js (legendary but deprecated)
Moment.js was the de facto standard for working with dates for a long time. It provides a rich API for parsing, validation, manipulation and formatting. However, the project is officially recognized as legacy — the developers recommend using modern alternatives. Main disadvantages: large size (~300 KB), mutating methods (change the original object) and problems with Tree Shaking.
// Installation: npm install momentconst moment = require('moment');
// Parsing and formattingconst now = moment();console.log(now.format('YYYY-MM-DD HH:mm:ss')); // 2024-01-15 14:30:00
// Manipulationsconst future = moment().add(7, 'days').startOf('week');console.log(future.format('dddd, MMMM Do YYYY')); // Monday, January 22nd 2024
// Difference between datesconst a = moment('2024-01-01');const b = moment('2024-01-10');console.log(b.diff(a, 'days')); // 92. Day.js (lightweight successor)
Day.js is a modern library with the same API as Moment.js, but with a size of only 2 KB. It is completely immutable (does not change original objects), supports plugins and works great with Tree Shaking. Perfect for projects where bundle size is important.
// Installation: npm install dayjsconst dayjs = require('dayjs');const relativeTime = require('dayjs/plugin/relativeTime');dayjs.extend(relativeTime);
const now = dayjs();console.log(now.format('DD.MM.YYYY')); // 15.01.2024
// Working with time zones (requires utc plugin)const utc = require('dayjs/plugin/utc');dayjs.extend(utc);const date = dayjs.utc('2024-01-15T12:00:00Z').local();console.log(date.format('HH:mm')); // 15:00 (depends on time zone)
// Relative timeconsole.log(dayjs('2023-12-25').fromNow()); // 3 weeks ago3. date-fns (functional approach)
date-fns is a library built on the principles of functional programming. Instead of a single object, it provides many pure functions. Each function is imported separately, which allows you to include only the necessary code. This is the most performant and modular library among those listed.
// Installation: npm install date-fnsconst { format, addDays, differenceInDays, startOfWeek } = require('date-fns');const { ru } = require('date-fns/locale');
const now = new Date();
// Formatting with localizationconsole.log(format(now, 'PPPP', { locale: ru })); // Monday, January 15th, 2024
// Manipulations (pure functions)const nextWeek = addDays(now, 7);console.log(format(nextWeek, 'yyyy-MM-dd')); // 2024-01-22
// Differenceconst start = new Date(2024, 0, 1);console.log(differenceInDays(now, start)); // 14
// Start of weekconst monday = startOfWeek(now, { weekStartsOn: 1 });console.log(format(monday, 'dd.MM.yyyy')); // 15.01.20244. Luxon (modern standard from the creators of Moment.js)
Luxon is a library from the same developers as Moment.js, but without its shortcomings. It uses native DateTime, Duration and Interval objects, is completely immutable, supports time zones and internationalization out of the box. Recommended for new projects.
// Installation: npm install luxonconst { DateTime, Duration, Inte