Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 783
Adam Weitzman Avatar asked Jun 29 '17 16:06

Adam Weitzman


Video Answer


1 Answers

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.

like image 58
JB Nizet Avatar answered Sep 19 '22 00:09

JB Nizet