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.
There are some shorthand keys as well if you're into that whole brevity thing.
moment().add(7, 'd');
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.
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]).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.
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
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.
Alternatively, you can use durations to add to moments.
var duration = moment.duration({'days' : 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');
moment().add(1.5, 'months') == moment().add(2, 'months')