Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 832
Raif Avatar asked Jun 02 '16 14:06

Raif


People also ask

How do you add time to a moment?

To add time, pass the key of what time you want to add, and the amount you want to add. moment(). add(7, 'days');

Is MomentJS being deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week. With that I began a journey during a Hackathon to replace moment in a core library at my company.

How do you add 30 days in a moment?

moment(). add(30, 'days');

Is MomentJS still used?

MomentJS is a widely used time and date formatting and calculation library.


1 Answers

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.

like image 170
GertG Avatar answered Oct 29 '22 17:10

GertG