Moment.js officially stops development and goes into maintenance, and suggests using other time libraries. Indicates that the date library will not add new features, make any major changes, fix long-standing known issues, or resolve bundle size issues. Official announcement address: momentjs.com/docs/#/-pro…

moment.js

A large and comprehensive time and date library, which makes it easy to calculate times and dates in JavaScript, has been downloaded over 12 million times per week and has been successfully used in millions of projects.

Moment.js has two big problems:

Mutable objects

A Moment object is a mutable object; simply put, it changes itself by adding and subtracting at any time. This design makes the code very uncontrollable, and it is easy to introduce hidden and difficult to debug bugs.

2. The package is too large

Because momnet.js packs all the functionality and supported languages into a single package, the size of the package is an exaggerated 280.9KB, and it’s not shaking for Tree. If you want to use time zone specific features, the package size is 467.6 kB. To put it simply, we might just need a.format method to format time, and the user would need to load hundreds of kB libraries.

Day.js

Day.js is a lightweight JavaScript library that handles times and dates. Day.js is designed as a minimalist alternative to moment.js. The API and usage are almost identical, and the learning cost of switching is extremely low.

(The latest version of Chrome developer tools has also begun recommending that users replace moment.js with a smaller library of its kind.)

The characteristics of

  • Same API and usage as moment.js

  • Immutable data

  • Support Chainable operation

  • Internationalization I18n

  • A tiny library only 2KB in size

  • Full browser compatibility

use

API address: day.js.org/docs/zh-CN/…

npm install dayjs    

import dayjs from 'dayjs'  

dayjs().format()
Copy the code

Moment, DAYJS and date-fNS libraries all meet common requirements, but have the following characteristics:

  • Moment has powerful functions but also the largest volume. Moment.js is 280.9KB, day.js is 7K. Date-fns can be minimized due to modular loading.

  

  • The interfaces of DayJS and moment are almost identical, the learning cost of switching between them is extremely low, and the interface styles of Date-FNS are quite different.

  • Dayjs is preferred if compatibility is considered. Date-fns is preferred if modularity is preferred.

conclusion

  • It is recommended that new projects use the day.js date library directly.
  • Whether to replace the old project with the new date library is considered according to the actual situation. (Moment.js is a powerful date library, which should not be replaced if it can be used on a large scale in older projects.)