Skip to content Skip to sidebar Skip to footer

MomentJs IsBetween() Function Is Not Returning Expected Result?

I am using momentjs for fetching current time. Then I need to check is it between the specified range or not. Let me show what I have done. I am using isBetween function that is re

Solution 1:

MomentJs will consider beforeTime and afterTime to be in the same day (since you are not passing any dates here).

If you are comparing between two different dates, you should include them in your Moment object.

Here is a working example of your code (The current time is 3:30 PM):

var now = '2019-01-01 ' + moment().format('LT');
var format = 'Y-m-d h:mm A';
var time = moment(now,format),
beforeTime = moment('2019-01-01 3:05 PM', format);
afterTime = moment('2019-01-02 6:33 AM', format);
console.log("time: ");
console.log(time);
console.log("beforeTime: ");
console.log(beforeTime);
console.log("afterTime:");
console.log(afterTime);

if (time.isBetween(beforeTime, afterTime)) {
    console.log('is between Isha and Fajr');
} else {
    console.log('is not between Isha and Fajr');
}

Post a Comment for "MomentJs IsBetween() Function Is Not Returning Expected Result?"