⏳ Modern JavaScript date utility library ⌛️

Overview

date-fns

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


It's like Lodash for dates

  • It has 200+ functions for all occasions.
  • Modular: Pick what you need. Works with webpack, Browserify, or Rollup and also supports tree-shaking.
  • Native dates: Uses existing native type. It doesn't extend core objects for safety's sake.
  • Immutable & Pure: Built using pure functions and always returns a new date instance.
  • TypeScript & Flow: Supports both Flow and TypeScript
  • I18n: Dozens of locales. Include only what you need.
  • and many more benefits
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. To install the package run:

npm install date-fns --save
# or with yarn
yarn add date-fns

Docs

See date-fns.org for more details, API, and other docs.


License

MIT © Sasha Koss

Comments
  • Add function to set locale globally

    Add function to set locale globally

    Hi,

    It would be great to have the possibility to call a function that set the locale globally, instead of having to wrap date-fns functions. Something similar to moment.locale() where we would pass the imported locale.

    Thanks !

    🙏 Feature request 
    opened by moimael 33
  • Tree shaking issue in 2.17

    Tree shaking issue in 2.17

    I'm using date-fns in two projects (without additional locales) and when I upgraded to 2.17 the bundle size went to ~200Kb.

    Checked with the webpack bundle analyzer, and all the functions seem to be there. I'm actually using just 7 functions with import syntax (ESM), so nothing changed from my side. If I rollback to 2.16.1, everything is OK again.

    Does it have to do with the Typescript addition? My projects are plain js.

    opened by tkat 32
  • Duration support

    Duration support

    Functions to work with durations, like http://momentjs.com/docs/#/durations/ also #277

    Seems like it is an anticipated feature: https://www.reddit.com/r/javascript/comments/5gl51e/a_modern_javascript_date_utility_library/daucfia/ https://www.reddit.com/r/javascript/comments/5gl51e/a_modern_javascript_date_utility_library/dat91yu/

    Not sure about API. If someone has suggestions, please post them here

    🎉 Feature 
    opened by leshakoss 31
  • Specify a time zone for format()?

    Specify a time zone for format()?

    I recently encountered a phenomenon in date-fns that broke a Node.js script of mine:

    const d = new Date('2017-01-23');
    console.log(format(d, 'YYYY-MM-DD'));
    

    This code logs 2017-01-23 in Germany, but 2017-01-22 in Seattle.

    How do I best fix this to always log 2017-01-23? Specify a time zone? How? It may make sense to warn about this in the docs.

    opened by rauschma 24
  • Reinstate ability to format time with

    Reinstate ability to format time with "am"/"pm"

    Under version 1, formatDate supported the following formatting options:

    Pattern|Result examples -- | -- A| AM, PM a | am, pm aa | a.m., p.m.

    but version 2 only has these options:

    Pattern|Result examples -- | -- a..aaa | AM, PM |   aaaa | a.m., p.m. aaaaa | a, p

    The style guide for the site I'm working on requires "am" and "pm" — lower case, no dots. Would it be possible to reinstate an option to have "am" and "pm"? (Perhaps as "aaa" if that doesn't break compatibility with anything…)

    I'd rather not have to mess about with reformatting the output from the formatter (if it was down to me, I'd format times in 24-hour clock and be done with it :)

    Thanks

    🌏 I18n ✋ Help wanted 🔥 Important 🙏 Feature request 
    opened by unikitty37 22
  • Adding one month results in 29 days

    Adding one month results in 29 days

    I'm using the addMonths function to add 1 month to a given date. The function is inside a forEach but that doesn't add nothing to the bug.

    Receiving the date 2019-03-01T01:00:00.000Z and adding 1 month to it dateFns.addMonths(actualDate, 1) (actualDate is the referred date object) results in 2019-04-29T01:00:00.000Z when it should result 2019-05-01T01:00:00.000Z. I tried starting in 2019-01-01 and add 1 month to each new date. Here are the resulting dates:

    2019-02-01T00:00:00.000Z
    2019-03-01T01:00:00.000Z
    2019-03-29T01:00:00.000Z
    2019-04-29T01:00:00.000Z
    2019-05-29T01:00:00.000Z
    2019-06-29T01:00:00.000Z
    2019-07-29T01:00:00.000Z
    2019-08-29T01:00:00.000Z
    2019-09-29T01:00:00.000Z
    2019-10-29T01:00:00.000Z
    

    Beginning in 2018-01-01, the resulting dates are:

    2018-02-01T00:00:00.000Z
    2018-03-01T01:00:00.000Z
    2018-03-29T01:00:00.000Z
    2018-04-29T01:00:00.000Z
    2018-05-29T01:00:00.000Z
    2018-06-29T01:00:00.000Z
    2018-07-29T01:00:00.000Z
    2018-08-29T01:00:00.000Z
    2018-09-29T01:00:00.000Z
    2018-10-29T01:00:00.000Z
    2018-11-29T00:00:00.000Z
    2018-12-29T00:00:00.000Z
    2019-01-29T00:00:00.000Z
    2019-03-01T01:00:00.000Z
    2019-03-29T01:00:00.000Z
    2019-04-29T01:00:00.000Z
    2019-05-29T01:00:00.000Z
    2019-06-29T01:00:00.000Z
    2019-07-29T01:00:00.000Z
    2019-08-29T01:00:00.000Z
    2019-09-29T01:00:00.000Z
    2019-10-29T01:00:00.000Z
    

    My nodejs version is 12.12.0 and date-fns version is 2.6.0

    ☠️ Won't fix 
    opened by otaviobps 21
  • Time zone helpers including IANA time zone support

    Time zone helpers including IANA time zone support

    I've built on the addition in #676 by @benmccann and tried to figure out what an API / helper functions for time zone support might look like which would solve the use cases presented in #180. Key additions are the two time zone helper functions:

    • setTimeZone for getting a Date instance with the time at a given time zone
    • dateAtTimeZone for getting a Date with the UTC value of a time in a given time zone

    The toDate, format and parse functions also have additional time zone support added.

    For a more complete discussion of the new functionality see the Time zone documentation included in this PR.

    The date-fns library aims to support IE8+ (see #660). This PR proposes using the Intl API for IANA time zone support which requires a polyfill in older browsers. As a compromise, all new time zone functionality fully supports the use of time zone offset values (+0200, -04:00 etc.) which will work back to IE8. If you wish to use IANA time zones a polyfill will be needed though.

    🌐 TZ 
    opened by marnusw 21
  • Incorrect result across DST in differenceInDays

    Incorrect result across DST in differenceInDays

    Calculating differenceInDays fails across DST

    Example: For London 2017-03-26, 01:00 clocks were turned forward 1 hour to 02:00 https://www.timeanddate.com/time/change/uk/london?year=2017

    const d1 = new Date(2017, 2, 27, 1);
    const d2 = new Date(2017, 2, 26, 2);
    differenceInDays(d1, d2) === 1 // should be '0'
    
    🐛 Bug 👩‍🔬 Needs triage 
    opened by standy 20
  • new locale: Occitan (closes #2061)

    new locale: Occitan (closes #2061)

    Hello! This is my first PR here. I struggle a bit to end it... For the "match" part and a JS function called ordinalNumber in Localize. If someone could give me some hints.

    Thanks!

    closes #2061

    🎉 Feature 🌏 I18n 
    opened by Mejans 19
  • Upgrade leads to 'Cannot augment module 'xxxx'  with value exports because it resolves to a non-module entity

    Upgrade leads to 'Cannot augment module 'xxxx' with value exports because it resolves to a non-module entity

    If I run tsc --noEmit --incremental false I get a long list of Cannot augment... error messages. Everything was fine with 2.10.0 but fails now with 2.11.0

    ...
    node_modules/date-fns/typings.d.ts:15311:9 - error TS2649: Cannot augment module 'ro' with value exports because it resolves to a non-module entity.
    
    15311   const ro: Locale
                  ~~
    
    node_modules/date-fns/typings.d.ts:15314:9 - error TS2649: Cannot augment module 'ru' with value exports because it resolves to a non-module entity.
    
    15314   const ru: Locale
                  ~~
    
    node_modules/date-fns/typings.d.ts:15317:9 - error TS2649: Cannot augment module 'sk' with value exports because it resolves to a non-module entity.
    ...
    
    🐛 Bug 
    opened by MikeMitterer 18
  • How to format utc like moment.js?

    How to format utc like moment.js?

    Is there any way to do like this by date-fns?

    let now = new Date().getTime();
    // after some time...
    moment.utc(new Date().getTime() - now).format(' ( HH:mm:ss )');
    

    date-fns has formatDistance() ,but I don't need the result in words.

    🤔 Feedback wanted 🙏 Feature request 
    opened by Hx2heng 18
  • add getIsoMonth - what month does this date categorize as, in the ISO way of counting.

    add getIsoMonth - what month does this date categorize as, in the ISO way of counting.

    I'm wondering if this function doesn't exist because there's some sort of inherit limitation to it? Or if it's just that no one else needs it?

    I'm looking for a getIsoMonth function.

    You give it a date, or a weekYear. It returns the month it belongs to (under ISO way of counting).

    examples: "2022-01-01" -> "December" "2022-03-01" -> "February" "2022-03-07" -> "March"

    also fine if the function returns: "2022-01-01" -> "2021-12"

    opened by stevenjchang 0
  • Fix errors in locale/is

    Fix errors in locale/is

    See commit messages for more info.

    I added a start of a test.ts file for the icelandic locale, focusing on the parsing patterns, which were badly broken.

    In addition to fixing typos in the matching and parsing patterns, I also made the matchers way (!) more forgiving/lenient in terms of the "width" of the mathced pattern, to better adhere to the Robustness Principle.

    opened by maranomynet 0
  • Fix week start for English (India)

    Fix week start for English (India)

    The start of the day for en-IN is Sunday rather than Monday as per the ISO 8601.

    Ref: https://metacpan.org/dist/DateTime-Locale/view/lib/DateTime/Locale/en_IN.pod#Local-first-day-of-the-week https://help.salesforce.com/s/articleView?id=000387765&type=1

    or

    try this in any browser console

    new Intl.Locale("en-in").weekInfo
    

    Screenshot 2023-01-02 at 9 45 23 PM

    opened by anand-prem 3
  • Added the right formatting for basic option and updated the test

    Added the right formatting for basic option and updated the test

    resolves #3291 As discussed in issue it was simple issue with the formatting of output and test was passing as it was also expecting the wrong fomrat.

    Made follwing changes in src/formatISO

    1. Updated the index.ts file in src/formatISO to check for the basic option and format the result without the collon.
    2. And updated the src/formatISO/test.ts to check for the right format without collon.
    opened by AhmadDecoded 0
Releases(v2.29.3)
Owner
date-fns
Modern JavaScript date utility library
date-fns
Create Persian Calendar as html helper with tag builder c# , and convert selected persian date to gregorian date

Persian-Calendar Use JS,Html,CSS,C# White theme for Persian Calendar , easy to use. Create Persian Calendar as html helper. Use Tag builder in c# for

Tareq Awwad 4 Feb 28, 2022
A date picker web component, and spiritual successor to duet date picker

<date-picker> A date picker web component, based on duet date picker, except authored with Lit and using shadow DOM. This is a work in progress. Todo:

Nick Williams 25 Aug 3, 2022
⏰ Day.js 2KB immutable date-time library alternative to Moment.js with the same modern API

English | 简体中文 | 日本語 | Português Brasileiro | 한국어 | Español (España) | Русский Fast 2kB alternative to Moment.js with the same modern API Day.js is a

null 41.7k Dec 28, 2022
CalendarPickerJS - A minimalistic and modern date-picker component/library 🗓️👨🏽‍💻 Written in Vanilla JS

CalendarPickerJS The simple and pretty way to let a user select a day! Supports all major browser. Entirely written in Vanilla JavaScript with no depe

Mathias Picker 15 Dec 6, 2022
⏰ Day.js 2kB immutable date-time library alternative to Moment.js with the same modern API

English | 简体中文 | 日本語 | Português Brasileiro | 한국어 | Español (España) | Русский Fast 2kB alternative to Moment.js with the same modern API Day.js is a

null 41.7k Dec 28, 2022
🕑 js-joda is an immutable date and time library for JavaScript.

js-joda is an immutable date and time library for JavaScript. It provides a simple, domain-driven and clean API based on the ISO8601 calendar.

null 1.5k Dec 27, 2022
:clock8: :hourglass: timeago.js is a tiny(2.0 kb) library used to format date with `*** time ago` statement.

timeago.js timeago.js is a nano library(less than 2 kb) used to format datetime with *** time ago statement. eg: '3 hours ago'. i18n supported. Time a

hustcc 4.9k Jan 4, 2023
This library provide a fast and easy way to work with date.

Calendar.js Calendar.js provide a fast way to work with dates when you don't wanna deal with hours, minute, second and so on. It means that Calendar.j

ActuallyNotaDev 3 Apr 27, 2022
DEPRECATED: Timezone-enabled JavaScript Date object. Uses Olson zoneinfo files for timezone data.

TimezoneJS.Date A timezone-enabled, drop-in replacement for the stock JavaScript Date. The timezoneJS.Date object is API-compatible with JS Date, with

Matthew Eernisse 830 Nov 20, 2022
A tiny and fast zero-dependency date-picker built with vanilla Javascript and CSS.

A tiny zero-dependency and framework-agnostic date picker that is incredibly easy to use! Compatible with any web UI framework, vanilla JS projects, and even HTML-only projects!

Nezar 1 Jan 22, 2021
Reusable date picker component for React

React DayPicker DayPicker is a reusable date picker component for React. $ npm install react-day-picker@next Beta version ⚠️ This branch is for the ne

Giampaolo Bellavite 4.8k Dec 28, 2022
Date() for humans

date Date is an english language date parser for node.js and the browser. For examples and demos, see: http://matthewmueller.github.io/date/ Update: d

Matthew Mueller 1.5k Jan 4, 2023
Lightweight and simple JS date formatting and parsing

fecha Lightweight date formatting and parsing (~2KB). Meant to replace parsing and formatting functionality of moment.js. NPM npm install fecha --save

Taylor Hakes 2k Jan 5, 2023
A lightweight, locale aware formatter for strings containing unicode date tokens.

Date Token Format A lightweight (~2kB), locale aware formatter for strings containing unicode date tokens. Usage Install the package using: yarn add d

Donovan Hutchinson 1 Dec 24, 2021
Nepali Date Picker jQuery Plugin 🇳🇵

Nepali Date Picker Nepali Date Picker jQuery Plugin for everyone. ???? Installation npm install nepali-date-picker Demo and Documentation https://leap

Leapfrog Technology 70 Sep 29, 2022
React Native Week Month Date Picker

React Native Week Month Date Picker Date picker with a week and month view Installation npm install react-native-week-month-date-picker Dependencies T

Noona 119 Dec 27, 2022
Easy to get a date.

date2data 테이블에 날짜별 데이터 넣을 때마다 새로 객체 만들어서 작업하기가 매우 귀찮아서 만들었다. moment.js를 쓰기에는 구현하고자 하는 내용이 너무 가벼웠음 Install npm i date2data Usage import {getMonthlyDate

Duho Kim 3 Apr 12, 2022
Tiny millisecond conversion utility

ms Use this package to easily convert various time formats to milliseconds. Examples ms('2 days') // 172800000 ms('1d') // 86400000 ms('10h')

Vercel 4.4k Jan 4, 2023
Tiny millisecond conversion utility

ms Use this package to easily convert various time formats to milliseconds. Examples ms('2 days') // 172800000 ms('1d') // 86400000 ms('10h')

Vercel 4.4k Jan 4, 2023