moment.js add doesn't add time

Raif picture Raif · Jun 2, 2016 · Viewed 7.8k times · Source

when I do

---- edit ----

var someMoment = moment('6:30 PM', ["h:mm A"]);

---- end edit ----

someMoment.add(30, 'minutes')  

I don't seem to get any result.

console.log(start); -- Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…}
console.log(start.add(inc, 'minutes')); --Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…}

The docs say that add mutates the specified moment, so the above should work but I've also tried

var end = start.add(inc, 'minutes')
console.log(end); --Moment {_isAMomentObject: true, _i: "6:30 PM", _f: "h:mm A", _isUTC: false, _pf: Object…}

what I can do though is this

console.log(start.add(inc, 'minutes').format("h:mm A")); --7:00 PM

What I want is to take a moment, add 30 minutes to it and, preferably have a new moment that is 30 minutes ahead, or at least have the initial moment be 30 minutes ahead.

I know I can take the format out put and put it in a new moment, and I guess I will but this seems kind of broken.

---- edit ----

using moment 2.1

I am getting this from within a method in my app, I haven't isolated it in a jsfiddle or anything, but the method takes a string and a increment. I guess I'll paste it here

here i'm trying one way but I've also tried using the modified start and cloning the start

var timeIsBetweenStartInc = function(_target:string, _start:string, inc:int){
var target = moment(_target, ["h:mm A"]);
var start = moment(_start, ["h:mm A"]);
console.log(start);
var end = moment(start).add(inc, 'minutes');
console.log(end);

return target.isBetween(start, end, 'minutes', '[)');(target, start, end);
};

---- end edit ----

Answer

GertG picture GertG · Jun 2, 2016

I suspect that Moment.js is working exactly like you expect it too, but console.log isn't. It prints a reference to the object, so the state you see is the current state at the time of consultation, not at the time of calling log(). Try using console.dir() instead to see the difference. Or use JSON.stringify() to get a string representation of the object, or call format() on the Moment object.