How to install date-fns

Created

date-fns provides the most comprehensive, yet simple and consistent toolset for manipulating JavaScript dates in a browser & Node.js.

date-fns is like lodash for dates. It has 140+ functions for all occasions.

date-fns includes some optional features as submodules in the npm package.

Installation

You can download the latest version of date-fns from the GitHub releases or use a date-fns CDN.

Cloning a repository

You can clone a repository from GitHub.com to your local computer.
Cloning with HTTPS URLs
git clone https://github.com/date-fns/date-fns.git
Cloning with SSH URLs
git clone [email protected]:date-fns/date-fns.git
Cloning with GitHub CLI
gh repo clone date-fns/date-fns

Install via NPM package

Run the following command to locally install the package and its dependencies with NPM.
Next Version:2.0.0-beta.5
Publish Time:2019-08-16 20:36:03
Unpacked Size:7.51 MB
Downloading and installing packages locally
npm i date-fns
npm install date-fns
yarn add date-fns
Downloading and installing packages globally
npm i -g date-fns
yarn global add date-fns
Install Specific Version of a Package
npm install [email protected]
Bower
bower install date-fns

Examples

Format date

import { format, formatDistance, formatRelative, subDays } from 'date-fns'

format(new Date(), "'Today is a' eeee")
//=> "Today is a Tuesday"

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

formatRelative(subDays(new Date(), 3), new Date())
//=> "last Friday at 7:26 p.m."

I18n

import { formatRelative, subDays } from 'date-fns'
import { es, ru } from 'date-fns/locale'

formatRelative(subDays(new Date(), 3), new Date())
//=> "last Friday at 7:26 p.m."

formatRelative(subDays(new Date(), 3), new Date(), { locale: es })
//=> "el viernes pasado a las 19:26"

formatRelative(subDays(new Date(), 3), new Date(), { locale: ru })
//=> "в прошлую пятницу в 19:26"

Composition & FP

import { addYears, formatWithOptions } from 'date-fns/fp'
import { eo } from 'date-fns/locale'

const addFiveYears = addYears(5)

const dateToString = formatWithOptions({ locale: eo }, 'D MMMM YYYY')

const dates = [
  new Date(2017, 0, 1),
  new Date(2017, 1, 11),
  new Date(2017, 6, 2)
]

const toUpper = arg => String(arg).toUpperCase()

const formattedDates = dates.map(addFiveYears).map(dateToString).map(toUpper)
//=> ['1 JANUARO 2022', '11 FEBRUARO 2022', '2 JULIO 2022']

Why date-fns?

  • checkModular - With the function-per-file style, you can pick just what you need and stop bloating your project with useless functionality.
  • checkNative Date - date-fns doesn't reinvent the wheel and uses the existing native type. Also, it doesn't extend core objects for safety sake.
  • checkImmutable & Pure - date-fns is built using pure functions and always returns a new date instance instead of changing the passed one.
  • checkTypeScript & Flow - date-fns supports both Flow and TypeScript. The typings are generated from the source code and bundled with the package, so they always up-to-date.