# date-fns — card for AI agents

Modern JavaScript date utility library with comprehensive formatting functions.

Category: Utilities · npm package `date-fns` · latest version 4.4.0 (checked 2026-11-01)
Install: `npm install date-fns`
License: MIT (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Rules for AI agents (paste into AGENTS.md or CLAUDE.md)

## date-fns
- Install with `npm install date-fns --save`. Import functions by name, as the README does: `import { compareAsc, format } from "date-fns"`.
- Use the value a date-fns call returns. The README says the library "always returns a new date instance".
- To sort dates, pass the comparator directly, e.g. `dates.sort(compareAsc)`, as in the README example.
- For year-month-day output, follow the README's token casing `"yyyy-MM-dd"`. Do not copy `'yyyy-mm-dd'` from blog posts without checking the token in the docs.
- For date-only values that must not shift for users in other time zones, consider the Atomic Object approach: build dates from a string with date-fns `parse` instead of `new Date()`.
- Include only the locales the app needs. The README says "Dozens of locales. Include only what you need."
- For CDN usage, use `@date-fns/cdn`. The `date-fns` CDN scripts are deprecated and will be removed in the next major release.

## What it is
- date-fns is a JavaScript date utility library, published as `date-fns` on npm under the MIT license. The GitHub repository was created on 2014-10-06. The README says it provides "the most comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js" and calls it "like Lodash for dates", with "200+ functions for all occasions".
- You call named functions such as `format` and `compareAsc` on ordinary `Date` values. The README's first example formats `new Date(2014, 1, 11)` as `'2014-02-11'` and passes `compareAsc` straight to `Array.prototype.sort`. The maintainers describe the library as modular, built using pure functions, and "100% TypeScript". A banner in the README announces v4.0 "with first-class time zone support".
- The latest release is v4.4.0. It introduces a separate `@date-fns/cdn` package and deprecates the CDN scripts inside `date-fns`, as a first step toward a smaller npm package.

## Caveats
- CDN scripts are deprecated: v4.4.0 deprecates the CDN scripts shipped in `date-fns`. They will be removed in the next major release, and in `v5.0.0-alpha.0` they are already gone. CDN users are told to move to `@date-fns/cdn`.
- CDN source maps already removed: v4.4.0 removed the CDN source maps from `date-fns`. The release notes say `@date-fns/cdn` still includes them.
- Package layout still changing: The maintainers say v4.4.0 allowed cutting the zipped package from `5.83 MB` to `3.96 MB`, and that v5.0.0-alpha.0 brings it to `2.89 MB`. They call this "just the first step" and say to expect further size reduction in v4 and v5.
- Check tokens before copying blog code: The Atomic Object post's parse call uses the pattern `'yyyy-mm-dd'` with lowercase `mm`. The README's format example uses `"yyyy-MM-dd"` with uppercase `MM` to get `'2014-02-11'`. Check the token in the docs before reusing the post's code.

## Numbers (AgentGid data)
- Gid Score: 86.3 (#26 of all tools, 1 of 408 in its category)
- Used in 648 public projects set up for AI coding agents (10.4%)
- Downloads last week: 127,238,007 (as of 2026-11-24), +26.6% over 4 weeks
- GitHub stars: 36,663 (as of 2026-11-24)
- Last push: 2026-10-26

## Often used with
Lucide React 82.4%, clsx 81.3%, Tailwind Merge 79.6%, Class Variance Authority 76.4%, Radix UI 69.9%, React 92.1%

## Checked alternatives (same job)
- Lodash: https://agentgid.com/tools/lodash/agent.md
- Day.js: https://agentgid.com/tools/dayjs/agent.md
- Moment.js: https://agentgid.com/tools/moment/agent.md

## Links
- AgentGid page: https://agentgid.com/tools/date-fns/
- Repository: https://github.com/date-fns/date-fns
- Source: https://github.com/date-fns/date-fns#readme
- Source: https://github.com/date-fns/date-fns
- Source: https://github.com/date-fns/date-fns/releases


## date-fns: format and calculate dates in JavaScript

date-fns is a free JavaScript package of small date helpers. Your app shows "2 days ago", counts working days to a delivery date or prints a date in German, French or Japanese. It works in the browser and in Node.js, and a companion package handles time zones.

### Where to use it
- "2 days ago" on comments: A feed or inbox shows how long ago each message came in, in the reader's language.
- Delivery dates without weekends: A shop shows "arrives Wednesday" by adding three working days to the order date and skipping Saturday and Sunday. (idea, not from the official pages)
- One meeting time for every city: An event page shows a 15:00 New York call as 21:00 in Berlin and 05:00 next day in Tokyo.
- Dates in the reader's language: The same date shows as words in English, German or another language; you load only the languages you use.
- Sort and compare dates in a list: Order bookings or posts by date before you show them.
- Dates in file names and reports: Exports get names like report-2026-11-13.csv in a fixed, sortable form.

### How to start
- Add date-fns to your project.

```
npm install date-fns --save
```

- Import only the helpers you use and call them on normal JavaScript dates.

```
import { formatDistance, subDays } from "date-fns";

formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true });
//=> "3 days ago"
```

- For time zones, add the companion package and use TZDate.

```
npm install @date-fns/tz --save
```


### License
Free to use, also in paid and closed products, under the MIT license. Keep the license notice when you copy or ship its files; no visible credit is needed.

### Good to know
- Time zones need the separate @date-fns/tz package; date-fns alone uses the time zone of the computer it runs on.
- Format letters follow the Unicode standard, not Moment.js; YYYY and DD mean other things than yyyy and dd.
- The ready-to-load script files inside the date-fns package are deprecated; pages that load it with a script tag should switch to @date-fns/cdn.
- The project is active; version 4.4.0 came out on 29 May 2026, and a version 5 test release followed.

Checked 2026-11-11 against: https://github.com/date-fns/date-fns/blob/main/README.md, https://github.com/date-fns/date-fns/blob/main/pkgs/core/docs/gettingStarted.md, https://github.com/date-fns/date-fns/blob/main/pkgs/core/docs/timeZones.md, https://github.com/date-fns/date-fns/blob/main/pkgs/core/docs/unicodeTokens.md, https://github.com/date-fns/date-fns/blob/main/pkgs/core/docs/i18n.md, https://github.com/date-fns/date-fns/blob/main/pkgs/core/docs/cdn.md, https://github.com/date-fns/date-fns/blob/main/pkgs/core/LICENSE.md, https://github.com/date-fns/date-fns/blob/main/pkgs/tz/README.md, https://blog.date-fns.org/v40-with-time-zone-support/, https://registry.npmjs.org/date-fns

### Mistakes to avoid
- Don't: format(date, 'YYYY-MM-DD') → Do: format(date, 'yyyy-MM-dd') (YYYY is the week-numbering year and DD the day of the year; in our run date-fns 4.4.0 threw a RangeError for this.)
- Don't: Adding date-fns-tz for time zones in a date-fns 4 project → Do: npm install @date-fns/tz and use TZDate or the in option (date-fns 4 has its own time zone package; the docs point to date-fns-tz only for older versions.)
- Don't: <script src="https://cdn.jsdelivr.net/npm/date-fns/cdn.min.js"> → Do: <script src="https://cdn.jsdelivr.net/npm/@date-fns/cdn@4.4.0/cdn.min.js"> (The CDN files inside date-fns are deprecated, log a notice and are removed in version 5.)
- Don't: Doing date math with new Date() on a server and expecting the user's local day → Do: Create a TZDate in the user's time zone, or pass in tz('Zone/Name') to the helper (Plain dates use the server's time zone, so summer-time shifts and day borders come out wrong for users elsewhere.)

### Questions
- Does date-fns handle time zones? Yes, since version 4, through the separate @date-fns/tz package and its TZDate class.
- Why does format(date, 'YYYY-MM-DD') throw an error? YYYY is the week-numbering year and DD the day of the year; use yyyy-MM-dd. date-fns refuses those tokens unless you turn them on.
- Does it change my Date objects? No. Every helper returns a new date and leaves the one you passed alone.
- Can I still use require() instead of import? Yes. Version 4 is ESM-first but still ships CommonJS files.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
