I try to check if a certain time in h:mm is before another time with same format. However, the console shows me either an error saying isBefore is not a function or it always print false. I use this code in Ionic 3/Angular.
If I code as follows, the console prints false. I tried different arguments instead of hour.
var start_time = moment('20:00', 'h:mm').format('LT');
var last_time = moment('21:59', 'h:mm').format('LT');
var check_last = moment(start_time).isBefore(last_time, 'hour');
console.log(check_last);
If I code as follows. As found on the link below. The console give me the error is
var start_time = moment('20:00', 'h:mm').format('LT');
var last_time = moment('21:59', 'h:mm').format('LT');
var check_last = start_time.isBefore(last_time, 'hour');
console.log(check_last);
Moment.js isBefore function not working as expected
On the official website of momentjs I didn't figure out how to compare two times. Can you please give me support?
Moment's
format()
returns a string so you can't useisBefore
on something like time.format('hh:mm'), that's the reason you are getting isBefore is not a function.Also as per the docs itself , moment().isBefore() has undefined behavior and should not be used! If the code runs fast the initial created moment would be the same as the one created in isBefore to perform the check, so the result would be false
Try it without formatting to get the desired output