Raif
Raif

Reputation: 9269

moment.js add doesn't add time

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 ----

Upvotes: 12

Views: 14043

Answers (3)

robertklep
robertklep

Reputation: 203529

It looks like you're trusting some internal properties of the Moment objects (_i) to contain the adjusted time. It may be that manipulations are lazily applied, or that those internals are tracking the original time, but once you actually do anything with the object (like calling .format() on it), it's showing the adjusted time.

EDIT: or you're using an outdated version of Moment.js. It looks like version 1 exhibits similar behaviour to what you're seeing. However, you're probably looking at the most recent (v2) documentation, hence the discrepancy between documentation and observed behaviour.

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.

var start  = moment();
var end    = moment(start).add(30, 'minutes');

console.log( start.toString() );
console.log(   end.toString() );

Upvotes: 2

pinjasaur
pinjasaur

Reputation: 418

Moment.js has a clone method that you could use like this:

var now = moment(),
    future = now.clone();

And then you could add some time to the cloned moment:

future.add(30, "minutes");

Now if you compare the UNIX timestamps of the two moments...

future.unix() - now.unix();

You'll notice they're 1800 seconds = 30 minutes apart.

Upvotes: 4

GertG
GertG

Reputation: 979

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.

Upvotes: 18

Related Questions