Moment.js 是一个很棒的时间和日期库,具有许多强大的功能和实用程序。但是,如果您正在处理性能敏感的Web应用程序,则由于其复杂的API和较大的捆绑包大小,可能会导致巨大的性能开销。

Moment.js的问题:
- 它高度基于OOP API,这使得它无法使用树抖动,从而导致巨大的捆绑包大小和性能问题。
 - 它是可变的,因为OOP API和非纯函数会导致错误:
https://github.com/moment/moment/blob/develop/src/test/moment/add_subtract.js#L244-L286 
如果您没有使用时区,而只使用了moment.js中的一些简单函数,这可能会使您的应用程序膨胀,因此被视为过度杀伤。dayjs有一个较小的核心,并且具有非常相似的API,因此它非常容易迁移。 date-fns 可以实现 tree-shaking and other benefits 因此它可以与React,Sinon.js和webpack等一起使用。请参阅 https://github.com/moment/moment/issues/2373 以获取有关原因和方式的更多信息。人们从moment.js切换到其他解决方案。
简要比较
| 名称 | 尺寸(gzip的) | Tree-shaking | Popularity | Methods richness | Pattern | Timezone Support | Locale | 
|---|---|---|---|---|---|---|---|
| Moment.js | 329K(69.6K) | No | 38k | High | OO | Good(moment-timezone) | 123 | 
| date-fns | 78.4k(13.4k) without tree-shaking | Yes | 13k | High | Functional | Not yet | 32 | 
| dayjs | 6.5k(2.6k) without plugins | No | 14k | Medium | OO | Not yet | 23 | 
开发者之声
Removed moment.js to replace with date-fns - build output reduced by 40%
—Jared Farago from webnode project.
—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.
—Sergey Petushkov, a javaScript developer from Moscow, Russia • Currently in Berlin, Germany.
ESLint插件
如果你正在使用ESLint, 你可以安装一个
插件来帮助你识别代码库中你没有(可能不需要)Moment.js的地方。
安装插件……
Install the plugin…
  | 
  | 
…然后更新您的配置
  | 
  | 
Quick Links
- 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
 
⚠️ Note that the provided examples of date-fns are for v2 which is in pre-release right now. See v1 docs for the current release.
Parse
String + Date Format
Return the date parsed from date string using the given format string.
  | 
  | 
String + Time Format
Return the date parsed from time string using the given format string.
  | 
  | 
String + Format + locale
Return the date parsed from string using the given format string and locale.
  | 
  | 
Get + Set
Millisecond / Second / Minute / Hour
Get the Millisecond/Second/Minute/Hour of the given date.
  | 
  | 
Set the Millisecond/Second/Minute/Hour of the given date.
  | 
  | 
Date of Month
Gets or sets the day of the month.
  | 
  | 
Day of Week
Gets or sets the day of the week.
  | 
  | 
Day of Year
Gets or sets the day of the year.
  | 
  | 
Week of Year
Gets or sets the week of the year.
  | 
  | 
Days in Month
Get the number of days in the current month.
  | 
  | 
Weeks in Year
Gets the number of weeks in the current year, according to ISO weeks.
  | 
  | 
Maximum of the given dates
Returns the maximum (most distant future) of the given date.
  | 
  | 
Minimum of the given dates
Returns the minimum (most distant future) of the given date.
  | 
  | 
Manipulate
Add
Add the specified number of days to the given date.
  | 
  | 
Subtract
Subtract the specified number of days from the given date.
  | 
  | 
Start of Time
Return the start of a unit of time for the given date.
  | 
  | 
End of Time
Return the end of a unit of time for the given date.
  | 
  | 
Display
Format
Return the formatted date string in the given format.
  | 
  | 
Time from now
Return time from now.
  | 
  | 
Time from x
Return time from x.
  | 
  | 
Difference
Get the unit of time between the given dates.
  | 
  | 
Query
Is Before
Check if a date is before another date.
  | 
  | 
Is Same
Check if a date is same another date.
  | 
  | 
Is After
Check if a date is after another date.
  | 
  | 
Is Between
Check if a date is between two other dates.
  | 
  | 
Is Leap Year
Check if a year is a leap year.
  | 
  | 
Is a Date
Check if a variable is a native js Date object.
  | 
  |