How to compare only HH:mm?
See original GitHub issueI 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:
- Created 10 years ago
- Reactions:27
- Comments:10 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
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.
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:
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:
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.
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:
and then check that:
As an aside,
moment()does the same thing asmoment(new Date()), so that should save you some text.