Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only want to compare TIME values MomentJS

After looking through some of the other MomentJS Questions and answers I'm still stumped as to how one would use moment to simply compare two different times.

I do not need (want) the day/date to be considered.

My use case is this: I'm reading a schedule (start/end times) from a config file. This is done using Node.js

Starttime = 6:30 PM
Endtime = 3:30 AM

var currentTime= moment();    // e.g. 11:00 pm
var starttime = moment('06:30 pm', "HH:mm a");
var endtime = moment('03:30 am', "HH:mm a");

amIBetween = currtime.isBetween(starttime , endtime);
console.log(amIBetween);   //  returns false

My scenario is technically spanning two days and I understand why it's false.

I need (expect) moment to return TRUE - i.e. that currtime isBeteen start and endtime and falls in that range.

Would I need to check for a time after 12AM and then add a day to make the check work? Any other suggestions for accomplishing this. I looked at moment-range which has 'contains ' function but with similar question for that.

I'm finding it hard to believe that it's this complex, but maybe it is :\

--

Here's further clarification that issue arises with spanning days, even when trying to be more explicit:

var currentTime= moment('11:00p', "HH:mm a");
var startTime = moment('06:00p', "HH:mm a");
var endTime = moment('03:30a', "HH:mm a");

currentTime.toString(); //"Fri Oct 28 2016 23:00:00 GMT-0400"
startTime.toString();   // "Fri Oct 28 2016 18:00:00 GMT-0400"
endTime.toString();    // "Fri Oct 28 2016 03:30:00 GMT-0400"

currentTime.isBetween(startTime, endTime);  // false
currentTime.isAfter(endTime) && currentTime.isBefore(startTime); //false
currentTime.isAfter(startTime) && currentTime.isBefore(endTime); //false

Seems kind of obvious that they'd be false since the day/date is considered by moment. This is what I'm trying to get around.

The following would work:

endTime.add(1, "days");
currentTime.isBetween(startTime, endTime);  // TRUE

This would mean however, that I'd need to check if the START TIME was before 12AM && the ENDTIME as after 12AM then add 1 day to ENDTIME. Kludge?

like image 629
rfossella Avatar asked Sep 14 '25 06:09

rfossella


1 Answers

After my own testing and looking at other's suggestions it sill appeared that disregarding DAY/DATE and trying to span days was an issue. I came up with this which is now working in my app.

isTimeBetween = function(aStartTime, anEndTime, aCurrTime)
{
    // you may pass in aCurrTime or use the *actual* current time
    var currentTime = !aCurrTime ? moment() : moment(aCurrTime, "HH:mm a");
    var startTime = moment(aStartTime, "HH:mm a");
    var endTime = moment(anEndTime, "HH:mm a");

    if (startTime.hour() >=12 && endTime.hour() <=12 )
    {
        endTime.add(1, "days");       // handle spanning days
    }

    var isBetween = currentTime.isBetween(startTime, endTime);

    /***  testing   
    startTimeString = startTime.toString();
    endTimeString = endTime.toString();
    currentTimeString = currentTime.toString();

    console.log(startTimeString);
    console.log(endTimeString);
    console.log(currentTimeString);
    console.log('\nis '+ currentTimeString  + ' between ' + 
              startTimeString + ' and ' + endTimeString + ' : ' 
              + isBetween);
    ****/
    return isBetween;
    }

isTimeBetween("06:30pm", "03:30am", "11:00pm");     //true      !! this is main use case
isTimeBetween("06:30pm", "10:30pm", "11:00pm");     //false
isTimeBetween("04:00am", "06:00am");                //true (e.g. current time is 5am
like image 83
rfossella Avatar answered Sep 16 '25 19:09

rfossella