subtract()


moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);

通过减去时间来改变原始的 moment。

这与 moment#add 完全相同,只是不增加时间,而是减去时间。

moment().subtract(7, 'days');

2.8.0 版本之前,还支持 moment#subtract(String, Number) 语法。 不推荐使用它,而使用 moment#subtract(Number, String)

moment().subtract('seconds', 1); // 废弃于 2.8.0
moment().subtract(1, 'seconds');

2.12.0 版本开始,当为日期和月份传入小数时,它们会被四舍五入到最接近的整数。 星期、季度、年份会被转换到日期或月份,然后四舍五入到最接近的整数。

moment().subtract(1.5, 'months') == moment().subtract(2, 'months')
moment().subtract(.7, 'years') == moment().subtract(8, 'months') //.7*12 = 8.4,取整到 8

注意,为了使操作 moment.add(-.5, 'days')moment.subtract(.5, 'days') 等价,-。5、-1.5、-2.5 等都向下舍入。

moment().subtract(Number, String);
moment().subtract(Duration);
moment().subtract(Object);

Mutates the original moment by subtracting time.

This is exactly the same as moment#add, only instead of adding time, it subtracts time.

moment().subtract(7, 'days');

Before version 2.8.0, the moment#subtract(String, Number) syntax was also supported. It has been deprecated in favor of moment#subtract(Number, String).

moment().subtract('seconds', 1); // Deprecated in 2.8.0
moment().subtract(1, 'seconds');

As of 2.12.0 when decimal values are passed for days and months, they are rounded to the nearest integer. Weeks, quarters, and years are converted to days or months, and then rounded to the nearest integer.

moment().subtract(1.5, 'months') == moment().subtract(2, 'months')
moment().subtract(.7, 'years') == moment().subtract(8, 'months') //.7*12 = 8.4, rounded to 8

Note that in order to make the operations moment.add(-.5, 'days') and moment.subtract(.5, 'days') equivalent, -.5, -1.5, -2.5, etc are rounded down.