Start a new topic

Modules.moment() isBefore() and isAfter() not Working

I've tried to use Modules.moment() within business logic, and cannot seem to get the isBefore() or isAfter() functions working.



I've tried with the following code:



function onRequest(request, response, modules){



var now = modules.moment();

var later = modules.moment().add('hours',1).add('days',1);



var output = "is Now before Later? by hour: " + now.isBefore(later, 'hours');

var output2 = "is Now before Later? in general: " + now.isBefore(later);

modules.logger.info(output + output2);



response.body = {"message": output+output2};

response.complete(200);

}



Any idea what the issue could be? Am I using the isBefore and isAfter incorrectly? I'm following the documentation provided by Kinvey and Moment.js.

Kinvey: http://devcenter.kinvey.com/phonegap/reference/business-logic/reference.html

Moment.js: http://momentjs.com/docs/#/query/is-before/



Thanks for your help anyone!

You. are. awesome.



Thanks that worked like a charm, sending the format() value instead of just the moment() object. Not sure why i didn't try that before!
Business Logic is currently using version 2.0.x of moment, which is not the most recent version (due to some compatibility issues that we are working through.) There is a bug in this version of moment where isBefore and isAfter will not actually take a moment as a parameter and return the correct result. You can achieve what you want by converting that moment into a string:



var result = modules.moment().isAfter(modules.moment().format()); // returns true



Note that isAfter is applicable here, not isBefore, because the inner expression is evaluated first, before the outer expression.
Thanks Fabricio. Good reasoning, and it makes sense...



But..... if that holds up then isAfter should not be 'false' as well, which when I try... it is.



Also... if I add an entire day I still get a false for isAfter.



var now = modules.moment();

var later = modules.moment().add('hours',1).add('days',1);

var output = "is Now before Later? by hour: " + now.isBefore(later);



now.isBefore(later) => false //no idea why....
I think if You run modules.moment().isSame(modules.moment()) you should get a false output. Thats cause the declaration inside the isSame is made in a different time that the first one. (millisecs...)
I keep getting false outputs on all isBefore, isAfter, and isSame calls no matter the input. I've even tried:



modules.moment().isSame(modules.moment())

-- and output => false



Login or Signup to post a comment