取值/赋值


Moment.js 使用重载的 getter 和 setter 方法。 此模式类似与其在 jQuery 中的使用。

不带参数调用这些方法会作为 getter,而带参数调用则会作为 setter。

这些会映射到原生 Date 对象上的相应函数。

moment().seconds(30).valueOf() === new Date().setSeconds(30);
moment().seconds()   === new Date().getSeconds();

如果处于 UTC 模式中,则它们将会映射到 UTC 的等效项。

moment.utc().seconds(30).valueOf() === new Date().setUTCSeconds(30);
moment.utc().seconds()   === new Date().getUTCSeconds();

为了方便起见,从 2.0.0 版本开始,单数和复数的方法名称都会存在。

注意:当作为 setter 使用时,所有这些方法在都会改变原始的 moment。

注意:从 2.19.0 开始,将 NaN 传给任何 setter 都是没有操作的。 在 2.19.0 之前,它以错误的方式使 moment 无效。

Moment.js uses overloaded getters and setters. You may be familiar with this pattern from its use in jQuery.

Calling these methods without parameters acts as a getter, and calling them with a parameter acts as a setter.

These map to the corresponding function on the native Date object.

moment().seconds(30).valueOf() === new Date().setSeconds(30);
moment().seconds()   === new Date().getSeconds();

If you are in UTC mode, they will map to the UTC equivalent.

moment.utc().seconds(30).valueOf() === new Date().setUTCSeconds(30);
moment.utc().seconds()   === new Date().getUTCSeconds();

For convenience, both singular and plural method names exist as of version 2.0.0.

Note: All of these methods mutate the original moment when used as setters.

Note: From 2.19.0 passing NaN to any setter is a no-op. Before 2.19.0 it was invalidating the moment in a wrong way.