你不需要Momentjs(译:你可能不需要Moment.js)
建议使用 date-fns 或者原生方法,取代 moment.js。如果你只是用momentjs
中的两三个方法,则完全可以考虑使用其他更加轻量、性能更加好的库,或者使用JavaScript
建议使用 momentjs
。
Moment.js
基本用法
moment(); //当前时间
moment().subtract(10, "days"); // 当前时间的前10天时间
moment().subtract(1, "years"); // 当前时间的前1年时间
moment().subtract(3, "months"); // 当前时间的前3个月时间
moment().subtract(1, "weeks"); // 当前时间的前一个星期时间
moment().format("YYYY-MM-DD HH:mm:ss"); // 转化为对应format格式
moment().valueOf() // 返回值为数值型,转为时间戳
startOf、endOf的方法
moment().startOf('year'); // 设置为今年的1月1日的00:00:00
moment().endOf('year'); // 设置为今年的12月31号的23:59:59
moment().startOf('month');
moment().endOf('month');
moment().startOf('quarter');
moment().endOf('quarter');
moment().startOf('week');
moment().endOf('week');
moment().startOf('isoWeek');
moment().endOf('isoWeek');
moment().startOf('day'); // 设置为今天的00:00:00;
moment().endOf('day'); // 设置为今天的23:59:59;
moment().startOf('date');
moment().endOf('date');
moment().startOf('hour');
moment().endOf('hour');
moment().startOf('minute');
moment().endOf('minute');
moment().startOf('second');
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 -
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 plugindate-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 libraryjs-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 withjava.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 usingDate
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 aMoment
object, it can not be set to use another time zone; It has no concept of “mode”.- Using
Date.parse
, ornew 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 theDate
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 theIntl.DateTimeFormat
constructor (and inDate.toLocaleString
,Date.toLocaleDateString
, andDate.toLocaleTimeString
). This option can be used to take the internal UTC-based timestamp of aDate
object and get a string that has been converted to a named time zone. However, it can not be used to convert aDate
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.