List of functions which you can use to replace moment.js + ESLint Plugin
List of functions which you can use to replace moment.js + ESLint Plugin
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:
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.
| 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) |
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.
—Matija Marohnić, a design-savvy frontend developer from Croatia.
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...
npm install --save-dev eslint-plugin-you-dont-need-momentjs...then update your config
"extends" : ["plugin:you-dont-need-momentjs/recommended"],⚠️ 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 | ✅ | ✅ | ✅ | ✅ | ✅ |
Return the date parsed from date string using the given format string.
…Return the date parsed from time string using the given format string.
…Return the date parsed from string using the given format string and locale.
…Get the Millisecond/Second/Minute/Hour of the given date.
// 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| 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.
…| Library | Time |
|---|---|
| Moment | 1689.744ms |
| Native | 636.741ms |
| DateFns | 714.148ms |
| DayJs | 2037.603ms |
| Luxon | 2897.571ms |
| Temporal | - |
Gets or sets the day of the month.
…| Library | Time |
|---|---|
| Moment | 1381.669ms |
| Native | 397.415ms |
| DateFns | 588.004ms |
| DayJs | 1218.025ms |
| Luxon | 2705.606ms |
| Temporal | - |
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 | - |
Gets or sets the day of the year.
…| Library | Time |
|---|---|
| Moment | 5511.172ms |
| Native | 530.592ms |
| DateFns | 2079.043ms |
| DayJs | - |
| Luxon | 3540.810ms |
| Temporal | - |
Gets or sets the week of the year.
…| Library | Time |
|---|---|
| Moment | 7147.201ms |
| Native | 1371.631ms |
| DateFns | 5834.815ms |
| DayJs | - |
| Luxon | 4514.771ms |
| Temporal | - |
Get the number of days in the current month.
// 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 TemporalFeature Parity is missing durations
It's a bit hard to set up using flat config style
Add js-joda
Thoughts on adding getting long month names (and possibly day names)?
from the docs it does not become clear what exactly the result is of using this plugin.
"[email protected]" has incorrect peer dependency "eslint@^5.5.0".
What about timeago.js ?
Unable to resolve dependency
How do I parse certain locale dates without MomentJS?
Worth discussing non-Gregorian calendar support in Moment alternatives