add()


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

通过增加时间来改变原始的 moment。

这是一个相当稳健的功能,可以为现有的 moment 增加时间。 若要增加时间,则传入要增加的时间的键、以及要增加的数量。

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

如果对希望简短,也有一些快捷的键。

moment().add(7, 'd');
快捷键
years y
quarters Q
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms

如果要同时增加多个不同的键,则可以将它们作为对象字面量传入。

moment().add(7, 'days').add(1, 'months'); // 链式
moment().add({days:7,months:1}); // 对象字面量

数量没有上限,因此可以重载任何参数。

moment().add(1000000, 'milliseconds'); // 一百万毫秒
moment().add(360, 'days'); // 360 天

月份和年份的特殊考虑

如果原始日期的月份中的日期大于最终月份中的天数,则该月份中的日期将会更改为最终月份中的最后一天。

moment([2010, 0, 31]);                  // 一月 31 号
moment([2010, 0, 31]).add(1, 'months'); // 二月 28 号

当增加跨越夏时制时间的时间时,还需要记住一些特殊的注意事项。 如果要增加年份、月份、周、或天,则原始的小时将始终与增加的小时匹配。

增加一个月会将指定的月数增加到日期。

moment([2010, 1, 28]);                 // 二月 28 号
moment([2010, 1, 28]).add(1, 'month'); // 三月 28 号
var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // 美国夏令时开始的前一天
m.hours(); // 5
m.add(1, 'days').hours(); // 5

如果要增加小时、分钟、秒钟或毫秒,则会假设期望精确到小时,这将会导致不同的小时。

var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // 美国夏令时开始的前一天
m.hours(); // 5
m.add(24, 'hours').hours(); // 6(但可能需要先设置时区)

另外,可以使用时长来增加时间。

var duration = moment.duration({'days' : 1});
moment([2012, 0, 31]).add(duration); // 二月 1 号

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

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

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

moment().add(1.5, 'months') == moment().add(2, 'months')
moment().add(.7, 'years') == moment().add(8, 'months') //.7*12 = 8.4,取整到 8
moment().add(Number, String);
moment().add(Duration);
moment().add(Object);

Mutates the original moment by adding time.

This is a pretty robust function for adding time to an existing moment. To add time, pass the key of what time you want to add, and the amount you want to add.

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

There are some shorthand keys as well if you're into that whole brevity thing.

moment().add(7, 'd');
Key Shorthand
years y
quarters Q
months M
weeks w
days d
hours h
minutes m
seconds s
milliseconds ms

If you want to add multiple different keys at the same time, you can pass them in as an object literal.

moment().add(7, 'days').add(1, 'months'); // with chaining
moment().add({days:7,months:1}); // with object literal

There are no upper limits for the amounts, so you can overload any of the parameters.

moment().add(1000000, 'milliseconds'); // a million milliseconds
moment().add(360, 'days'); // 360 days

Special considerations for months and years

If the day of the month on the original date is greater than the number of days in the final month, the day of the month will change to the last day in the final month.

moment([2010, 0, 31]);                  // January 31
moment([2010, 0, 31]).add(1, 'months'); // February 28

There are also special considerations to keep in mind when adding time that crosses over daylight saving time. If you are adding years, months, weeks, or days, the original hour will always match the added hour.

Adding a month will add the specified number of months to the date.

moment([2010, 1, 28]);                 // February 28
moment([2010, 1, 28]).add(1, 'month'); // March 28
var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US
m.hours(); // 5
m.add(1, 'days').hours(); // 5

If you are adding hours, minutes, seconds, or milliseconds, the assumption is that you want precision to the hour, and will result in a different hour.

var m = moment(new Date(2011, 2, 12, 5, 0, 0)); // the day before DST in the US
m.hours(); // 5
m.add(24, 'hours').hours(); // 6 (but you may have to set the timezone first)

Alternatively, you can use durations to add to moments.

var duration = moment.duration({'days' : 1});
moment([2012, 0, 31]).add(duration); // February 1

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

moment().add('seconds', 1); // Deprecated in 2.8.0
moment().add(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().add(1.5, 'months') == moment().add(2, 'months')
moment().add(.7, 'years') == moment().add(8, 'months') //.7*12 = 8.4, rounded to 8