How to compare only HH:mm?

See original GitHub issue

I am using moment.js with my node.js project

isWithinTime: function(end) {

     var moment = require('moment');
     var now = moment(new Date()).format('HH:mm');
     var end = moment(new Date(end)).format('HH:mm');

     if (now.toString() > end.toString()) {
         return false;
     }
     return true;
}

I want to compare only time in HH:mm format. However it does not work because

'7:00' > '2:00'

returns false. Any ideas how to compare HH:mm?

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:27
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

51reactions
rfossellacommented, Nov 5, 2016

After looking through other MomentJS questions I’m still stumped as to how one would use moment to simply compare two different times

I need (want) the day/date to be ignored.

Use case: I’m reading a schedule w/ start & end times from a config file. This is done using Node.js

Start time = 6:30 PM
End time = 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 = currentTime.isBetween(startTime , endTime);
console.log(amIBetween);   //  returns false.  if date ignored I expect TRUE

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

I need (expect) moment to return TRUE - i.e. that currentTime isBeteen startTime and endTime and falls in that range.

I thought I had it solved by checking if endTime hour is > 12 then add a day to make moment understand it’s the following day. e.g.

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

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

But if currentTime is also after midnight then it breaks - e.g.

01:30am will not be considered between 06:30pm and 03:30am - also because of different days

Any other suggestions for accomplishing this and simply ignoring the day/date?

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.

48reactions
icambroncommented, Oct 20, 2013

Usually, if you don’t care about the end time’s date, it’s better to just keep track of the hours and minutes explicitly, but of course I don’t know anything about your app. So to answer it directly, define a new function for returning the number of minutes:

var minutesOfDay = function(m){
  return m.minutes() + m.hours() * 60;
}

and then check that:

return minutesOfDay(now) > minutesOfDay(end);

As an aside, moment() does the same thing as moment(new Date()), so that should save you some text.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I compare two time strings in the format HH:MM:SS?
Date object in js support comparison, set them same date for compare ... Below code only works if the time is in HH:MM:SS...
Read more >
Compare two Time strings formatted as HH:MM:SS in JS
Use string comparison to compare two time strings, that are formatted as hh:mm:ss , e.g. if (time1 > time2) {} . The time...
Read more >
How to compare HH:MM in C# - MSDN
Hi I have to compare HH:MM(hour and minutes). How can i do so? String hourMinute; hourMinute = DateTime ...
Read more >
Comparing time difference in HH:mm:ss - Ignition
I would need to display the time difference of After minus Before in 'HH:mm:ss' format. I am currently using this script:
Read more >
SQL Dates & Times | BigQuery Syntax and Examples
One of the most common things we use dates for is to compare them. We'll do this to filter for the last week...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found