Esta página se muestra en inglés. Una traducción al español está en curso.
ConfigsApr 11, 2026·2 min de lectura

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.

Introducción

date-fns is a modern JavaScript date utility library with 200+ functions. Unlike Moment.js (which is mutable and class-based), date-fns is functional, immutable, and tree-shakeable — only the functions you import ship in your bundle. The modern, efficient choice for date handling in TS/JS.

What date-fns Does

  • Format — 50+ format tokens, locale-aware
  • Parse — strict parsing, ISO, custom formats
  • Arithmetic — add/sub days, months, years, hours, minutes, weeks
  • Comparison — isAfter, isBefore, isEqual, closestTo
  • Difference — differenceInDays/Hours/Minutes
  • Distance — formatDistance, formatDistanceToNow (relative strings)
  • Locales — 70+ locales
  • Timezone — via date-fns-tz adapter
  • Immutable — every function returns a new Date

Architecture

Pure functions over native Date objects — no classes, no wrappers. Each function is a separate module. Locales are separate imports for tree-shakeability. Types are native TypeScript (not @types).

Self-Hosting

Client or server library, zero runtime deps.

Key Features

  • 200+ pure functions
  • Tree-shakeable (v2+)
  • TypeScript native
  • 70+ locales
  • Immutable
  • Small (~15KB with common functions)
  • No Moment-style mutable object
  • ESM and CJS builds
  • Native Date compatible

Comparison

Library Size API Style Tree-shake Timezone
date-fns ~15KB used Functional Yes Via plugin
Day.js ~6KB Chainable (Moment-like) Plugins Via plugin
Luxon ~30KB OO (classes) No Built-in
Moment ~230KB Mutable classes No Built-in
Temporal (TC39) Native Modern API N/A Native

FAQ

Q: date-fns or Day.js — which should I choose? A: date-fns is more functional and tree-shakes better; Day.js feels more like Moment with a smoother chainable API. It comes down to personal preference.

Q: How do I handle timezones? A: Use the date-fns-tz companion package, which provides zonedTimeToUtc, formatInTimeZone, and more.

Q: Will I not need it once Temporal ships? A: Temporal isn't fully landed yet (Safari has partial support) and the polyfill is ~60KB. date-fns will remain mainstream for the foreseeable future.

Sources & Credits

Discusión

Inicia sesión para unirte a la discusión.
Aún no hay comentarios. Sé el primero en compartir tus ideas.

Activos relacionados