你不需要Momentjs(译:你可能不需要Moment.js

建议使用 date-fns 或者原生方法,取代 moment.js。如果你只是用momentjs中的两三个方法,则完全可以考虑使用其他更加轻量、性能更加好的库,或者使用JavaScript建议使用 momentjs

Moment.js

官网
中文网

基本用法

  1. moment(); //当前时间
  2. moment().subtract(10, "days"); // 当前时间的前10天时间
  3. moment().subtract(1, "years"); // 当前时间的前1年时间
  4. moment().subtract(3, "months"); // 当前时间的前3个月时间
  5. moment().subtract(1, "weeks"); // 当前时间的前一个星期时间
  6. moment().format("YYYY-MM-DD HH:mm:ss"); // 转化为对应format格式
  7. moment().valueOf() // 返回值为数值型,转为时间戳

startOf、endOf的方法

  1. moment().startOf('year'); // 设置为今年的1月1日的00:00:00
  2. moment().endOf('year'); // 设置为今年的12月31号的23:59:59
  3. moment().startOf('month');
  4. moment().endOf('month');
  5. moment().startOf('quarter');
  6. moment().endOf('quarter');
  7. moment().startOf('week');
  8. moment().endOf('week');
  9. moment().startOf('isoWeek');
  10. moment().endOf('isoWeek');
  11. moment().startOf('day'); // 设置为今天的00:00:00;
  12. moment().endOf('day'); // 设置为今天的23:59:59;
  13. moment().startOf('date');
  14. moment().endOf('date');
  15. moment().startOf('hour');
  16. moment().endOf('hour');
  17. moment().startOf('minute');
  18. moment().endOf('minute');
  19. moment().startOf('second');
  20. moment().endOf('second');

(2020.09.18)moment.js 宣布停止开发,进入维护状态

你仍然可以继续使用moment,但是也推荐使用其他方案。
下面是官方给出的替代方案

Luxon

Luxon can be thought of as the evolution of Moment. It is authored by Isaac Cambron, a long-time contributor to Moment. Please read Why does Luxon exist? and the For Moment users pages in the Luxon documentation.

  • Locales: Intl provided
  • Time Zones: Intl provided

    Day.js

    Day.js is designed to be a minimalist replacement for Moment.js, using a similar API. It is not a drop-in replacement, but if you are used to using Moment’s API and want to get moving quickly, consider using Day.js.

  • Locales: Custom data files that can be individually imported

  • Time Zones: Intl provided, via a plugin

    date-fns

    Date-fns offers a series of functions for manipulating JavaScript Date objects. For more details, scroll to “Why date-fns?” on the date-fns home page.

  • Locales: Custom data files that can be individually imported

  • Time Zones: Intl provided, via a separate companion library

    js-Joda

    js-Joda is a JavaScript port of Java’s Three-Ten Backport, which is the base for JSR-310 implementation of the Java SE 8 java.time package. If you are familiar with java.time, Joda-Time, or Noda Time, you will find js-Joda comparable.

  • Locales: Custom data files via add-on module

  • Time Zones: Custom data files via add-on module

    No Library

    JavaScript has always had a Date object, defined ECMAScript (ECMA-262) specification here.
    When using Date objects, be aware of the following:

  • The Date object internally represents a Unix timestamp with millisecond precision. It offers functions that will convert to and from the system’s local time zone, but it is always UTC internally. Unlike a Moment object, it can not be set to use another time zone; It has no concept of “mode”.

  • Using Date.parse, or new Date(<string>) has been problematic and implemented inconsistently in the past. The current specification defines parsing a variation of ISO 8601 strings, where date-only forms (like "2020-09-14") are parsed as UTC, instead of local time as they would be by ISO 8601. Even then, not all modern implementations have implemented this specification correctly (e.g., Safari). Other types of strings may work, but parsing them is implementation specific and can vary significantly - especially with older browsers. Depending on the implementation, and the components provided in the string, you may be surprised with the result. For these reasons, we agree with MDN’s statement that parsing strings with the Date object is strongly discouraged.

Modern JavaScript environments will also implement the by ECMA-402 specification, which provides the Intl object, and defines behavioral options of the Date object’s toLocaleString, toLocaleDateString, and toLocaleTimeString functions.
When using the Intl object, be aware of the following:

  • Not every environment will implement the full specification. In particular, Node.js environments require internationalization support provided by ICU. See the Node.js documentation for further details.
  • The ECMAScript Intl compatibility table (by kangax) can be useful in determining which features are supported and which are not.
  • Most newer environments provide IANA time zone support via the timeZone option in the Intl.DateTimeFormat constructor (and in Date.toLocaleString, Date.toLocaleDateString, and Date.toLocaleTimeString). This option can be used to take the internal UTC-based timestamp of a Date object and get a string that has been converted to a named time zone. However, it can not be used to convert a Date object to a different time zone.

If the Date and Intl objects meet your needs and you fully understand their limitations, then you might consider using them directly.

参考资料

https://www.jianshu.com/p/9c10543420de