Working with Dates in TypeScript: Overview of date-fns, Luxon, and Day.js Libraries

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Working with Dates in TypeScript: Overview of Popular Libraries



Working with dates and times is one of the most common yet non-trivial tasks in web development. The built-in Date object in JavaScript (and TypeScript) has a number of drawbacks: mutability, a non-intuitive API, timezone issues, and a lack of convenient formatting methods. Fortunately, the TypeScript ecosystem offers several powerful libraries that solve these problems and provide excellent type support.



In this article, we will look at three of the most popular libraries: date-fns, Luxon, and Day.js. You will learn about their features, advantages, disadvantages, and see usage examples with TypeScript.



1. date-fns — Modular and Functional



date-fns is a library built on the principles of functional programming. Each function is a separate module, allowing you to import only what you need and significantly reduce bundle size. In TypeScript, the library has built-in types.



Installation

npm install date-fns


Usage Examples with TypeScript

import { format, addDays, differenceInDays, isAfter, parseISO } from 'date-fns';

// Formatting a dateconst today = new Date();console.log(format(today, 'yyyy-MM-dd')); // '2025-03-20'

// Adding daysconst nextWeek = addDays(today, 7);console.log(format(nextWeek, 'dd.MM.yyyy')); // '27.03.2025'

// Difference between datesconst startDate = parseISO('2025-01-01');const endDate = parseISO('2025-12-31');const daysBetween = differenceInDays(endDate, startDate);console.log(`Days in 2025: ${daysBetween}`); // 364

// Comparing datesconst isFuture = isAfter(parseISO('2025-06-01'), today);console.log(`Is June 1, 2025 in the future? ${isFuture}`); // true


Advantages

  • Tree-shaking — you import only the functions you need, reducing application size.
  • Immutability — functions do not modify the original objects but return new ones.
  • TypeScript Support — full typing out of the box.
  • Localization — support for many languages, including Russian.


Disadvantages

  • Works with the native Date object, which can be inconvenient for complex timezone operations.
  • No built-in support for immutable objects (like in Luxon).


2. Luxon — Powerful and Modern



Luxon is a library from one of the authors of Moment.js, but without its drawbacks. It uses its own immutable DateTime, Duration, and Interval objects, making date manipulation intuitive and safe. Luxon has excellent support for timezones and locales.



Installation

npm install luxon

For TypeScript, you may need to install types:

npm install --save-dev @types/luxon


Usage Examples with TypeScript

import { DateTime, Duration } from 'luxon';

// Creation and formattingconst now = DateTime.now();console.log(now.toFormat('yyyy-MM-dd HH:mm:ss')); // '2025-03-20 14:30:00'

// Working with timezonesconst moscowTime = DateTime.now().setZone('Europe/Moscow');console.log(`Moscow time: ${moscowTime.toFormat('HH:mm')}`);

// Adding an intervalconst nextMonth = now.plus({ months: 1 });console.log(`In a month: ${nextMonth.toFormat('dd.MM.yyyy')}`);

// Difference between datesconst start = DateTime.fromISO('2025-01-01');const end = DateTime.fromISO('2025-12-31');const diff = end.diff(start, ['months', 'days']);console.log(`Difference: ${diff.months} months and ${diff.days} days`);

// Working with durationsconst duration = Duration.from

Recommendations