操作


一旦有了 Moment,则可能需要以某些方式对其进行操作。 有很多方法可以帮助处理此需求。

Moment.js 使用流式的接口模式,也称为方法链。 这使得可以执行以下疯狂的操作。

moment().add(7, 'days').subtract(1, 'months').year(2009).hours(0).minutes(0).seconds(0);

注意:moment 是可变的。 调用任何一种操作方法都会改变原始的 moment。

如果要创建副本并对其进行操作,则应在操作 moment 之前使用 moment#clone查看有关克隆的更多信息

Once you have a Moment, you may want to manipulate it in some way. There are a number of methods to help with this.

Moment.js uses the fluent interface pattern, also known as method chaining. This allows you to do crazy things like the following.

moment().add(7, 'days').subtract(1, 'months').year(2009).hours(0).minutes(0).seconds(0);

Note: It should be noted that moments are mutable. Calling any of the manipulation methods will change the original moment.

If you want to create a copy and manipulate it, you should use moment#clone before manipulating the moment. More info on cloning.