What is this library and why is it needed
Moment.js and Day.js are two powerful JavaScript libraries designed for working with dates and times. They solve a fundamental problem: the native Date object in JavaScript is extremely inconvenient for everyday tasks. It does not support formatting, complex date calculations, parsing strings in different formats, or working with time zones without writing dozens of lines of code.
Moment.js was the de facto standard for a long time, but is now considered "legacy" — its development is frozen, and it is not recommended for new projects due to its large size (about 300 KB). It has been succeeded by Day.js — a lightweight (only 2 KB) and fast library with a nearly identical API. Day.js is fully compatible with Moment.js for most basic operations, making migration easy.
Both libraries provide:
- Parsing dates from strings in any format
- Formatting dates into human-readable strings
- Manipulations (adding/subtracting days, months, years)
- Date comparison (before, after, between)
- Time zone support (via plugins)
- Localization (support for different languages and regional formats)
In this article, we will focus on Day.js as a modern alternative, but all examples will work with Moment.js with minimal changes.
Installation
Installation for Node.js is done via npm or yarn. For the browser, you can use a CDN.
Installing Day.js
# Via npmnpm install dayjs
# Via yarnyarn add dayjsInstalling Moment.js
npm install moment
# oryarn add momentImporting in a project
// Day.jsconst dayjs = require('dayjs');
// Moment.jsconst moment = require('moment');Quick start with code example
After installation, you can start working with dates immediately. Here is a minimal example demonstrating the main features:
const dayjs = require('dayjs');
// Current date and timeconst now = dayjs();console.log('Now:', now.format('DD.MM.YYYY HH:mm:ss'));
// Parsing a stringconst date = dayjs('2024-03-15');console.log('Parsing:', date.format('DD.MM.YYYY'));
// Manipulationsconst nextWeek = dayjs().add(7, 'day');console.log('Next week:', nextWeek.format('DD.MM.YYYY'));
// Difference between datesconst diff = nextWeek.diff(now, 'day');console.log('Difference in days:', diff);
// Comparisonconsole.log('Is today earlier?', now.isBefore(nextWeek)); // trueMain methods/functions
1. dayjs() — creating a date object
What it does: Creates a Day.js object representing a specific point in time. Without arguments, it returns the current date and time.
Parameters:
date(optional) — a string, number (timestamp), Date object, or another Day.js objectformat(optional) — a format string for parsing, if date is a string
Working code example:
// Current dateconst now = dayjs();
// From a stringconst fromString = dayjs('2024-12-31');
// From a timestamp (milliseconds)const fromTimestamp = dayjs(1704067200000);
// From a Date objectconst fromDate = dayjs(new Date(2024, 0, 1)); // January 1, 2024
// With format specificationconst customFormat = dayjs('31.12.2024', 'DD.MM.YYYY');
console.log(now.format()); // 2024-03-15T10:30:00+03:00console.log(customFormat.format('YYYY-MM-DD')); // 2024-12-312. format() — date formatting
What it does: Converts the date object into a string according to a given