I 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"));
Answer from Arvind Kumar Avinash on Stack Overflownpm
npmjs.com › package › date-fns
date-fns - npm
import { compareAsc, format } from "date-fns"; format(new Date(2014, 1, 11), "yyyy-MM-dd"); //=> '2014-02-11' const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ] The library is available as an npm package.
» npm install date-fns
date-fns
date-fns.org
date-fns - modern JavaScript date utility library
date-fns provides the most comprehensive yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.
Videos
GitHub
github.com › date-fns › date-fns
GitHub - date-fns/date-fns: ⏳ Modern JavaScript date utility library ⌛️
import { compareAsc, format } from "date-fns"; format(new Date(2014, 1, 11), "yyyy-MM-dd"); //=> '2014-02-11' const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ] The library is available as an npm package.
Author date-fns
npm
npmjs.com › package › date-fns-tz
date-fns-tz - npm
Time zone support for date-fns v3 with the Intl API. Latest version: 3.2.0, last published: a year ago. Start using date-fns-tz in your project by running `npm i date-fns-tz`. There are 1941 other projects in the npm registry using date-fns-tz.
» npm install date-fns-tz
Published Sep 30, 2024
Version 3.2.0
Author Marnus Weststrate
Repository https://github.com/marnusw/date-fns-tz
Top answer 1 of 2
5
I 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"));
2 of 2
0
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);
npm
npmjs.com › package › date-fns-jalali
date-fns-jalali - npm
import { compareAsc, format, newDate } from "date-fns-jalali"; format(new Date(2014, 1, 11), "yyyy-MM-dd"); //=> '1392-11-22' newDate(1392, 10, 22); // => Tue Feb 11 2014 00:00:00 format(newDate(1392, 10, 22), "yyyy MMMM d"); // 1392 بهمن 22 const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ] The library is available as an npm package.
» npm install date-fns-jalali
Published Sep 26, 2024
Version 4.1.0-0
Best of JS
bestofjs.org › projects › date-fns
Best of JS • date-fns
import { compareAsc, format } from "date-fns"; format(new Date(2014, 1, 11), "yyyy-MM-dd"); //=> '2014-02-11' const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ] The library is available as an npm package.
GitHub
github.com › date-fns › date-fns › issues › 980
Error: Can't resolve 'date-fns/format' · Issue #980 · date-fns/date-fns
November 8, 2018 - Why am I getting: Module not found: Error: Can't resolve 'date-fns/format' after installing date-fns like this: npm install date-fns --save I am requiring it like this: const format = require('date-fns/format'); I can see this in my pack...
Published Nov 08, 2018
Libraries.io
libraries.io › npm › date-fns
date-fns 4.1.0 on npm - Libraries.io - security & maintenance data for open source software
import { compareAsc, format } from "date-fns"; format(new Date(2014, 1, 11), "yyyy-MM-dd"); //=> '2014-02-11' const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ] The library is available as an npm package.
npm
npmjs.com › package › @date-io › date-fns
@date-io/date-fns - npm
Latest version: 3.2.1, last published: a year ago. Start using @date-io/date-fns in your project by running `npm i @date-io/date-fns`. There are 969 other projects in the npm registry using @date-io/date-fns.
» npm install @date-io/date-fns
Published Mar 07, 2025
Version 3.2.1
Author Dmitriy Kovalenko
Repository https://github.com/dmtrKovalenko/date-io
npm
npmjs.com › package › ngx-date-fns
ngx-date-fns - npm
A collection of utilities built around date-fns functions. This pipe is (impure), but there's the pure version dfnsFormatRelativeToNowPure too. Same as dfnsFormatRelative but the date to comapre against is always Date.now(). ... Given a weekday number, returns its name. Optional weekday format.
» npm install ngx-date-fns
Published May 14, 2025
Version 12.0.1
Snyk
snyk.io › advisor › date-fns › date-fns code examples
Top 5 date-fns Code Examples | Snyk
export const ScopeDays = (props: ScopeDaysProps) => { const { tasks, date } = props; const days = groupBy(tasks, t => formatDate(parseDate(t.date.split(' ')[0]))); const start = startOfMonth(date); let end = endOfMonth(date); const now = new Date(); if (end > now) { end = now; } if (end > start) { // Ensure that days before the current day are given a scope, even with no tasks eachDayOfInterval({ start, end }).forEach(day => { const key = formatDate(day); if (!days[key]) days[key] = []; }); }
GitHub
github.com › date-fns › date-fns › blob › main › docs › gettingStarted.md
date-fns/docs/gettingStarted.md at main · date-fns/date-fns
import { format, compareAsc } from "date-fns"; format(new Date(2014, 1, 11), "MM/dd/yyyy"); //=> '02/11/2014' const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ] date-fns includes some optional features as submodules in the npm package.
Author date-fns
DevDocs
devdocs.io › date_fns
DevDocs — date-fns documentation
date-fns 2.29.2 API documentation with instant search, offline support, keyboard shortcuts, mobile version, and more.
Runkit
npm.runkit.com › date-fns
RunKit + npm: date-fns
This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including date-fns with all npm packages installed.
Npm
npm.io › package › date-fns
Date-fns NPM | npm.io
import { compareAsc, format } from "date-fns"; format(new Date(2014, 1, 11), "yyyy-MM-dd"); //=> '2014-02-11' const dates = [ new Date(1995, 6, 2), new Date(1987, 1, 11), new Date(1989, 6, 10), ]; dates.sort(compareAsc); //=> [ // Wed Feb 11 1987 00:00:00, // Mon Jul 10 1989 00:00:00, // Sun Jul 02 1995 00:00:00 // ] The library is available as an npm package.