date-fns is a modern, lightweight, and modular JavaScript library for manipulating and formatting dates in both browsers and Node.js environments. It offers over 200 functions for common date operations, designed with a functional programming approach that ensures immutability and pure functions, returning new date instances instead of modifying existing ones.
Key Features:
Modular & Tree-shakeable: Import only the functions you need (e.g.,
import { format, addDays } from 'date-fns'), reducing bundle size.TypeScript Support: Fully typed with handcrafted types, no additional dependencies required.
I18n Ready: Supports dozens of locales; include only what’s needed.
Native Date Usage: Works with standard JavaScript
Dateobjects without extending core prototypes.Active Maintenance: Regular updates, including v4.0 with first-class time zone support via
date-fns-tz.
Installation:
npm install date-fnsCommon Usage:
import { format, addDays } from 'date-fns'; const tomorrow = addDays(new Date(), 1); console.log(format(tomorrow, 'dd/MM/yyyy')); // e.g., "10/03/2026"vs. Moment.js: date-fns is a superior alternative—smaller, faster, immutable, and actively maintained. Moment.js is deprecated.
Resources:
Official Site: date-fns.org
GitHub: github.com/date-fns/date-fns
Documentation: devdocs.io/date_fns
Playgrounds: Available on YouTube and third-party tools for experimentation.
» npm install date-fns
Videos
How do I format a date using Date-fns and Moment.js?
What is the difference between Moment.js and Date-fns?
When should I use Moment.js instead of Date-fns?
Hi everyone, I'm in the process of choosing a date library for our site rewrite, and a long time ago used date-fn, I quite liked the library, so I was going to install it but while navigating the page I got to the sponsors section and there is a lot of casinos and like farms advertising, and by the looks of it, spending big books paying for the space in the front page.
My question is: Is the library safe to use? Or was it compromised?
Sponsors in the pageI managed to run it successfully by using require as shown below:
const fns = require('date-fns')
console.log(fns.format(new Date(), "'Today is a' eeee"))
Update:
I installed node v16.6.1 following the instructions in this answer and now I can run the following code successfully:
import { format } from 'date-fns';
console.log(format(new Date(), "yyyy-MM-dd'T'HH:mm:ss.SSS"));
You are probably facing syntax errors, as you directly copy pasted the code from the documentation. Try importing the library as follows. It should work just fine.
import { format, formatDistance, formatRelative, subDays } from 'date-fns';
const mDate= format(new Date(2014, 1, 11), 'MM/dd/yyyy');
console.log(mDate);