Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
Y

You-Dont-Need-Momentjs

> 编程语言
Open source

List of functions which you can use to replace moment.js + ESLint Plugin

13.3K stars0 likes2 views
WebsiteGitHub

About

List of functions which you can use to replace moment.js + ESLint Plugin

You don't (may not) need Moment.js

Moment.js is a fantastic time & date library with lots of great features and utilities. However, if you are working on a performance sensitive web application, it might cause a huge performance overhead because of its complex APIs and large bundle size.

Problems with Moment.js:

  • It is highly based on OOP APIs, which makes it fail to work with tree-shaking, thus leading to a huge bundle size and performance issues.
  • It is mutable and it causes bugs:
    • clone
    • How do I work around mutability in moment.js?
  • Complex OOP API (which doubles mutability problem). Here is an example: https://github.com/moment/moment/blob/develop/src/test/moment/add_subtract.js#L244-L286 Moment.js allows to use a.subtract('ms', 50), a.subtract(50, 'ms') and even a.subtract('s', '50').

If you are not using timezone but only a few simple functions from moment.js, this might bloat your app, and therefore is considered overkill. dayjs has a smaller core and has very similar APIs so it makes it very easy to migrate. date-fns enables tree-shaking and other benefits so that it works great with React, Sinon.js, and webpack, etc. See https://github.com/moment/moment/issues/2373 for more ideas on why and how people switch from moment.js to other solutions.

Brief Comparison

Name Tree-shaking Methods richness Pattern Locale Timezone Support Popularity (stars) Sizes
Moment.js No High OO 123 Good (moment-timezone)
Luxon No High OO - Good (Intl)
date-fns Yes High Functional 64 Good (date-fns-tz)
dayjs No High OO 138 Good (Intl)

Voice of Developers

Removed moment.js to replace with date-fns - build output reduced by 40%

—Jared Farago from webnode project.

Good library if you’re looking to replace Moment.js for one reason or another. Immutable too.

—Dan Abramov, Author of Redux and co-author of Create React App. Building tools for humans.

I strongly recommend using date-fns over Moment.js, it's has a nicer API and you can include only parts you need!

—Matija Marohnić, a design-savvy frontend developer from Croatia.

ESLint Plugin

If you're using ESLint, you can install a plugin that will help you identify places in your codebase where you don't (may not) need Moment.js.

Install the plugin...

bash
npm install --save-dev eslint-plugin-you-dont-need-momentjs

...then update your config

javascript
"extends" : ["plugin:you-dont-need-momentjs/recommended"],

Quick Links

Parse

  1. String + Date Format
  2. String + Time Format
  3. String + Format + locale

Get + Set

  1. Millisecond/Second/Minute/Hour
  2. Date of Month
  3. Day of Week
  4. Day of Year
  5. Week of Year
  6. Days in Month
  7. Weeks in Year
  8. Maximum of the given dates
  9. Minimum of the given dates

Manipulate

  1. Add
  2. Subtract
  3. Start of Time
  4. End of Time

Display

  1. Format
  2. Time from now
  3. Time from X
  4. Difference

Query

  1. Is Before
  2. Is Same
  3. Is After
  4. Is Between
  5. Is Leap Year
  6. Is a Date

Feature Parity

⚠️ Indicates other packages or work are needed. See individual functions above.

Native Luxon date-fns dayjs Temporal
Parse
String + Date Format ✅ ✅ ✅ ✅ ✅
String + Time Format ✅ ✅ ✅ ⚠️ ✅
String + Format + locale ❌ ⚠️ ✅ ⚠️ ❌
Get + Set
Millisecond/Second/Minute/Hour ✅ ✅ ✅ ✅ ✅
Date of Month ✅ ✅ ✅ ✅ ✅
Day of Week ✅ ✅ ✅ ✅ ✅
Day of Year ✅ ✅ ✅ ✅ ✅
Week of Year ✅ ✅ ✅ ⚠️ ✅
Days in Month ✅ ✅ ✅ ✅ ✅
Weeks in Year ❌ ❌ ✅ ⚠️ ✅
Maximum of the given dates ✅ ✅ ✅ ⚠️ ✅
Minimum of the given dates ✅ ✅ ✅ ⚠️ ✅
Manipulate
Add ✅ ✅ ✅ ✅ ✅
Subtract ✅ ✅ ✅ ✅ ✅
Start of Time ❌ ✅ ✅ ✅ ✅
End of Time ✅ ✅ ✅ ✅ ✅
Display
Format ✅ ✅ ✅ ✅ ✅
Time from now ✅ ❌ ✅ ⚠️ ✅
Time from X ❌ ❌ ✅ ⚠️ ✅
Difference ✅ ✅ ✅ ✅ ✅
Query
Is Before ✅ ✅ ✅ ✅ ✅
Is Same ✅ ✅ ✅ ✅ ✅
Is After ✅ ✅ ✅ ✅ ✅
Is Between ❌ ✅ ✅ ⚠️ ❌
Is Leap Year ✅ ✅ ✅ ⚠️ ✅
Is a Date ✅ ✅ ✅ ✅ ✅

Parse

String + Date Format

Return the date parsed from date string using the given format string.

…

⬆ back to top

String + Time Format

Return the date parsed from time string using the given format string.

…

⬆ back to top

String + Format + locale

Return the date parsed from string using the given format string and locale.

…

⬆ back to top

Get + Set

Millisecond / Second / Minute / Hour

Get the Millisecond/Second/Minute/Hour of the given date.

javascript
// Moment.js
moment().seconds();
// => 49
moment().hours();
// => 19

// Native
new Date().getSeconds();
// => 49
new Date().getHours();
// => 19

// date-fns
import getSeconds from 'date-fns/getSeconds';
import getHours from 'date-fns/getHours';
getSeconds(new Date());
// => 49
getHours(new Date());
// => 19

// dayjs
dayjs().second();
// => 49
dayjs().hour();
// => 19

// Luxon
DateTime.local().second;
// => 49
DateTime.local().hour;
// => 19

// Temporal
Temporal.Now.zonedDateTimeISO().second;
// => 49
Temporal.Now.zonedDateTimeISO().hour;
// => 19

Performance tests

Library Time
Moment 1500.703ms
Native 348.411ms
DateFns 520.670ms
DayJs 494.234ms
Luxon 1208.368ms
Temporal -

Set the Millisecond/Second/Minute/Hour of the given date.

…

Performance tests

Library Time
Moment 1689.744ms
Native 636.741ms
DateFns 714.148ms
DayJs 2037.603ms
Luxon 2897.571ms
Temporal -

⬆ back to top

Date of Month

Gets or sets the day of the month.

…

Performance tests

Library Time
Moment 1381.669ms
Native 397.415ms
DateFns 588.004ms
DayJs 1218.025ms
Luxon 2705.606ms
Temporal -

⬆ back to top

Day of Week

Gets or sets the day of the week.

…
Library Time
Moment 1919.404ms
Native 543.466ms
DateFns 841.436ms
DayJs 1229.475ms
Luxon 3936.282ms
Temporal -

⬆ back to top

Day of Year

Gets or sets the day of the year.

…
Library Time
Moment 5511.172ms
Native 530.592ms
DateFns 2079.043ms
DayJs -
Luxon 3540.810ms
Temporal -

⬆ back to top

Week of Year

Gets or sets the week of the year.

…
Library Time
Moment 7147.201ms
Native 1371.631ms
DateFns 5834.815ms
DayJs -
Luxon 4514.771ms
Temporal -

⬆ back to top

Days in Month

Get the number of days in the current month.

javascript
// Moment.js
moment('2012-02', 'YYYY-MM').daysInMonth();
// => 29

// Native
new Date(2012, 02, 0).getDate();
// => 29

// date-fns
import getDaysInMonth from 'date-fns/getDaysInMonth';
getDaysInMonth(new Date(2012, 1));
// => 29

// dayjs
dayjs('2012-02').daysInMonth();
// => 29

// Luxon
DateTime.local(2012, 2).daysInMonth;
// => 29

// Temporal
(new Temporal

Issues· 33 open

View all issuesOpen on GitHub
  • #89

    Feature Parity is missing durations

    PR WelcomeUpdated Jan 18, 2025
  • #155

    It's a bit hard to set up using flat config style

    Updated Jan 7, 2025
  • #121

    Add js-joda

    Updated Dec 16, 2023
  • #65

    Thoughts on adding getting long month names (and possibly day names)?

    enhancementhelp wantedUpdated Oct 21, 2023
  • #142

    from the docs it does not become clear what exactly the result is of using this plugin.

    Updated Oct 18, 2023
  • #124

    "[email protected]" has incorrect peer dependency "eslint@^5.5.0".

    Updated Oct 18, 2023
  • #90

    What about timeago.js ?

    PR WelcomeUpdated Jul 27, 2023
  • #108

    Unable to resolve dependency

    Updated Jul 23, 2023
  • #139

    How do I parse certain locale dates without MomentJS?

    Updated Feb 24, 2023
  • #126

    Worth discussing non-Gregorian calendar support in Moment alternatives

    Updated May 31, 2022

> Tags

JavaScriptbundle-sizedate-fnsesmoduleshacktoberfest

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 2, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言