ConfigsApr 11, 2026·2 min read

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.

TL;DR
date-fns offers 200+ tree-shakeable functions for date parsing, formatting, arithmetic, and locale support.
§01

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.

§02

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.

§03

How to use

  1. Install date-fns:
npm i date-fns
  1. 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
  1. 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'
§04

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'));
});
§05

Related on TokRepo

§06

Common pitfalls

  • date-fns works with native JavaScript Date objects, which are mutable and timezone-unaware. For timezone handling, use the date-fns-tz companion package.
  • The format function 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-fns package in older bundlers may pull the entire library. Use direct imports like import { format } from 'date-fns' with a modern bundler for proper tree shaking.

Frequently Asked Questions

How does date-fns compare to Moment.js?+

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.

Does date-fns support timezones?+

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.

Is date-fns TypeScript-compatible?+

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.

How large is date-fns in a production bundle?+

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.

Can date-fns handle locale-specific formatting?+

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)

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets