Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding days to Moment object (momentjs)

I'm trying to find a way to add days to a moment. I can get it to work for a moment based on the current time, but that's it. Here my current code:

let start = moment('2017-01-15');
console.log(moment().add(7, 'days'));
console.log(moment(start).add(7, 'days'));

This is what get as the result:

Moment {_isAMomentObject: true, _isUTC: false, _pf: Object, _locale: Locale, _d: Mon Mar 13 2017 12:21:00 GMT-0400 (Eastern Daylight Time)…}

Moment {_isAMomentObject: true, _i: "2017-01-15", _f: "YYYY-MM-DD", _isUTC: false, _pf: Object…}

So it works on moment() but not that's it. Everywhere I look, that's how I'm supposed to do it so I don't know what I'm missing.

like image 521
Troy Taylor Avatar asked Dec 18 '25 00:12

Troy Taylor


1 Answers

You already created the moment object, now you can call add with start variable.

let start = moment('2017-01-15');
console.log(moment().add(7, 'days'));
console.log(start.add(7, 'days'));
.as-console-wrapper {
  top: 0;
  max-height: 100% !important;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
like image 197
Đào Minh Hạt Avatar answered Dec 20 '25 12:12

Đào Minh Hạt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!