Momentjs add doesn't work

Viewed 3296
 var startDate = moment("2017-06-30 00:00 +0000", "YYYY-MM-DD HH:mm Z");
 var endDate = startDate.clone();
 endDate.add(2, 'days');
 console.log('startDate', startDate);
 console.log('endDate', endDate);

I'm trying to add 2 days to the startdate but the endDate remains the same as the startdate. Am I doing something wrong?

1 Answers

To add days you can simpley use moment().add(2) here 2 is the number of days you wish to add

 var startDate = moment("2017-06-30 00:00 +0000", "YYYY-MM-DD HH:mm Z");
 var endDate = startDate.clone();
 endDate.add(2); // or use endDate.add(2, 'day')
 console.log('startDate', startDate);
 console.log('endDate', endDate);
<script src="https://momentjs.com/downloads/moment.js"></script>

If you like to add months or year just give the second parameter as months or years

 var startDate = moment(); // assign today
 console.log('After 2 days:', startDate.add(2, 'days')); // startDate.add(2, 'day') 
 console.log('After 2 months:', moment().add(2, 'months')); // or startDate.add(2, 'month') 
 console.log('After 2 years:', moment().add(2, 'years')); // or startDate.add(2, 'year') 
<script src="https://momentjs.com/downloads/moment.js"></script>

Related