Working with Dates in TypeScript: Library Overview
Working with dates is one of the most common tasks in web development. In TypeScript (and JavaScript), the built-in Date object has limited capabilities and an inconvenient API. Fortunately, there are many libraries that simplify formatting, parsing, comparison, and manipulation of dates. In this article, we will look at four popular libraries: date-fns, Day.js, Luxon, and Moment.js, and show examples of their use in TypeScript.
1. date-fns — Modular and Lightweight
date-fns is a modern library built on the principle of modularity. You import only the functions you need, which keeps the bundle size minimal. date-fns has excellent TypeScript support out of the box.
Installation:
npm install date-fns# oryarn add date-fnsUsage Examples:
import { format, addDays, differenceInDays, parseISO } from 'date-fns';
// Formattingconst today = new Date();console.log(format(today, 'yyyy-MM-dd')); // 2025-03-15
// Adding daysconst future = addDays(today, 10);console.log(format(future, 'dd.MM.yyyy')); // 25.03.2025
// Difference between datesconst start = parseISO('2025-01-01');const end = parseISO('2025-03-15');console.log(differenceInDays(end, start)); // 73
// Weekend checkimport { isWeekend } from 'date-fns';console.log(isWeekend(new Date(2025, 2, 15))); // true (Saturday)2. Day.js — Lightweight Alternative to Moment.js
Day.js is a minimalist library (only 2 kB) with an API very similar to Moment.js. It is ideal if you want to migrate from Moment but reduce bundle size. Day.js supports TypeScript through types.
Installation:
npm install dayjs# oryarn add dayjsUsage Examples:
import dayjs from 'dayjs';import relativeTime from 'dayjs/plugin/relativeTime';import utc from 'dayjs/plugin/utc';
dayjs.extend(relativeTime);dayjs.extend(utc);
// Creation and formattingconst now = dayjs();console.log(now.format('DD/MM/YYYY HH:mm')); // 15/03/2025 14:30
// Relative timeconsole.log(dayjs('2025-03-10').fromNow()); // 5 days ago
// Working with UTCconst utcDate = dayjs.utc('2025-03-15T12:00:00Z');console.log(utcDate.local().format('HH:mm')); // local time
// Manipulationsconst nextMonth = dayjs().add(1, 'month');console.log(nextMonth.format('MMMM YYYY')); // April 20253. Luxon — Powerful and Modern
Luxon is the successor to Moment.js, developed by the same team. It uses the native DateTime object, supports time zones, intervals, and durations. Luxon is written in TypeScript and has excellent typing.
Installation:
npm install luxon# oryarn add luxonUsage Examples:
import { DateTime, Interval, Duration } from 'luxon';
// Creation and formattingconst dt = DateTime.now();console.log(dt.toFormat('yyyy LLL dd')); // 2025 Mar 15
// Time zonesconst ny = DateTime.now().setZone('America/New_York');console.log(ny.toLocaleString(DateTime.DATETIME_FULL)); // March 15, 2025 at 9:30 AM EDT
// Intervalsconst start = DateTime.fromISO('2025-01-01');const end = DateTime.fromISO('2025-03-15');const interval = Interval.fromDateTimes(start, end);console.log(interval.length('days')); // 73
// Durationsconst dur = Duration.fromObject({ hours: 5, minutes: 30 });console.log(dur.toHuman()); // 5 hours, 30 minutes
// Parsing