Modern JavaScript date utility library
open sourceMITself-hostablefree
date-fns is a library of more than 200 functions for working with JavaScript dates (formatting, parsing, arithmetic, comparisons, differences, intervals, start and end of periods) in browsers and Node.js. It works on native Date objects rather than a wrapper type, every function is pure and returns a new date, and the functions are separate modules, so bundlers keep only the ones you import. It is written in TypeScript, ships dozens of locales that are included only when imported, and has curried versions of its functions in date-fns/fp. Since v4 (September 2024) time zones come from its own @date-fns/tz (TZDate) and @date-fns/utc (UTCDate) packages or each function's in option. Sasha Koss created it in 2014.
Problems it solves
- import only the date functions I use so my JavaScript bundle stays small
- format and parse dates in JavaScript without wrapping them in a custom date object
- count the business days between two dates in TypeScript
- date helpers that never mutate the Date I pass in, for React state
- adding hours across a daylight saving change gives the wrong time in the user's zone
- calculate dates in a specific time zone in Node.js, not the server's system zone
- check whether two date ranges overlap in JavaScript
- list every day in a month to render a calendar grid
- show a localized date like 'Tuesday, 3 March' in many languages
- compose date helpers in a functional pipeline with curried functions
- get the ISO week number of a date for a weekly report
- well-typed date utilities for a TypeScript codebase
Features
- more than 200 functions: format, parse, add, sub, differenceIn*, isBefore, eachDayOfInterval, startOf*, endOf* and more
- works on native Date objects, no wrapper type
- pure, immutable functions that return a new date
- one module per function, tree-shakable; ESM-first with CommonJS builds
- written in TypeScript
- dozens of locales, included only when imported
- date-fns/fp: curried functions for composition
- time zones with TZDate (@date-fns/tz) and UTCDate (@date-fns/utc), or the in option on each function
- CDN builds in the @date-fns/cdn package
Capabilities
| Has an API | plan not known |
|---|
Alternatives
The same job
These do what date-fns does.
