Adam Weitzman
Adam Weitzman

Reputation: 1622

Add Days With Moment JS

Having trouble adding days to moment js objects:

I'm using this code:

var contractMoment = this.moment(contract,'DD/MM/YYYY')
var start = contractMoment;
var end = contractMoment;

start = contractMoment.add(19, 'days');
end = contractMoment.add(51, 'days');

contractMoment looks like this before I add:

Thu Dec 02 2004 00:00:00 GMT-0600 (Central Standard Time)

and after I do the adding and console log start and end, here is what I'm getting:

Thu Dec 02 2004 00:00:00 GMT-0600 (Central Standard Time)

It returns a moment object for each, what am I missing here? is the added date buried somewhere in the moment object?

Upvotes: 7

Views: 22083

Answers (1)

JB Nizet
JB Nizet

Reputation: 691685

The add() method doesn't return a new moment. It modifies the moment and returns it. You need to create copies:

var contractMoment = moment(contract, 'DD/MM/YYYY');
var start = moment(contractMoment).add(19, 'days');
var end = moment(contractMoment).add(51, 'days');

See http://plnkr.co/edit/PgQuFARXGUB4fxUOxEYN?p=preview for a demo.

Upvotes: 19

Related Questions