date-fns — Modern Modular JavaScript Date Utility Library
date-fns is a modern JavaScript date utility library providing 200+ functions for parsing, formatting, arithmetic, and comparison. Tree-shakeable, immutable, TypeScript-first, and the modern alternative to Moment.js.
What it is
date-fns is a modern JavaScript date utility library providing over 200 functions for parsing, formatting, arithmetic, and comparison of dates. It is tree-shakeable (import only what you use), immutable (never mutates the input date), and TypeScript-first. It serves as the modern alternative to Moment.js, which is now in maintenance mode.
date-fns is for frontend and backend JavaScript/TypeScript developers who need reliable date manipulation without pulling in a large, monolithic library.
How it saves time or tokens
Native JavaScript Date methods are inconsistent and limited. Formatting a date as 'April 15, 2026' requires manual string concatenation. Calculating 'how many business days between two dates' requires writing custom logic. date-fns provides these as one-line imports.
Because date-fns is tree-shakeable, your production bundle only includes the functions you actually use. A project that imports format and addDays ships only those two functions, not the entire 200+ function library.
How to use
- Install date-fns:
npm i date-fns
- Import and use specific functions:
import { format, formatDistanceToNow, addDays, isAfter, parseISO } from 'date-fns';
const now = new Date();
// Format a date
format(now, 'yyyy-MM-dd HH:mm'); // '2026-04-15 14:30'
// Relative time
formatDistanceToNow(parseISO('2026-04-01'), { addSuffix: true });
// 'about 14 days ago'
// Arithmetic
const tomorrow = addDays(now, 1);
isAfter(tomorrow, now); // true
- Use with locales for internationalization:
import { format } from 'date-fns';
import { de } from 'date-fns/locale';
format(new Date(), 'PPPP', { locale: de });
// 'Mittwoch, 15. April 2026'
Example
Calculating business days and formatting for a report:
import {
eachDayOfInterval,
isWeekend,
format,
startOfMonth,
endOfMonth,
} from 'date-fns';
const start = startOfMonth(new Date());
const end = endOfMonth(new Date());
const businessDays = eachDayOfInterval({ start, end })
.filter(day => !isWeekend(day));
console.log(`Business days this month: ${businessDays.length}`);
businessDays.forEach(day => {
console.log(format(day, 'EEE, MMM d'));
});
Related on TokRepo
- Coding AI tools -- JavaScript development tools
- Automation tools -- utility libraries for automation
Common pitfalls
- date-fns works with native JavaScript Date objects, which are mutable and timezone-unaware. For timezone handling, use the
date-fns-tzcompanion package. - The
formatfunction uses Unicode tokens (yyyy, MM, dd), not Moment.js tokens (YYYY, MM, DD). Migrating from Moment.js requires updating format strings. - Importing from the root
date-fnspackage in older bundlers may pull the entire library. Use direct imports likeimport { format } from 'date-fns'with a modern bundler for proper tree shaking.
Frequently Asked Questions
date-fns is tree-shakeable and immutable; Moment.js is monolithic and mutates dates in place. Moment.js is in maintenance mode and recommends alternatives like date-fns for new projects. date-fns produces smaller bundles because you import only the functions you need.
The core date-fns library works with the local timezone of the JavaScript runtime. For explicit timezone handling (converting between zones, formatting in specific zones), use the date-fns-tz companion package.
Yes. date-fns is written in TypeScript and ships with full type definitions. Every function has typed parameters and return values, providing autocomplete and type checking in your editor.
With tree shaking, date-fns adds only the functions you import. A typical usage (format, parse, addDays, isAfter) adds roughly 5-10KB gzipped. Without tree shaking, the full library is about 80KB gzipped.
Yes. date-fns supports 60+ locales. Import the locale you need and pass it as an option to formatting functions. Each locale is a separate module, so unused locales are not included in your bundle.
Citations (3)
- date-fns GitHub— date-fns modern JavaScript date utility library
- date-fns Documentation— Function reference and format tokens
- Moment.js Project Status— Moment.js maintenance mode recommendation
Related on TokRepo
Discussion
Related Assets
Conda — Cross-Platform Package and Environment Manager
Install, update, and manage packages and isolated environments for Python, R, C/C++, and hundreds of other languages from a single tool.
Sphinx — Python Documentation Generator
Generate professional documentation from reStructuredText and Markdown with cross-references, API autodoc, and multiple output formats.
Neutralinojs — Lightweight Cross-Platform Desktop Apps
Build desktop applications with HTML, CSS, and JavaScript using a tiny native runtime instead of bundling Chromium.