10

I need to check if a time is between a start and end time. All times are on the same day, so date is not important. I'm able to compare hours, but I'm unsure how to add in minutes to the start and end times.

var thedate = new Date(); 
var dayofweek = thedate.getUTCDay(); 
var hourofday = thedate.getUTCHours(); 
var minutesofday = date.getMinutes();
function inTime() { if (dayofweek != 0 && dayofweek != 7 && (hourofday > 13 && hourofday < 20)) { return true; } return false; } 

If I want to check whether the time is between 13:05 and 19:57, how would I add the minutes to my inTime function? If I add them to the if statement, it fails to work:

 function inTime() { if (dayofweek != 0 && dayofweek != 7 && ((hourofday > 13 && minutesofday > 5) && (hourofday < 20 && minutesofday < 57))) { return true; } return false; } 
8
  • I recommend you checkout momentjs library momentjs.com. It has method to check time. Please refer to this stackoverflow.com/questions/23620498/… Commented Apr 4, 2018 at 15:49
  • 1
    Really? The downvoter should rethink why he is on this site. #SOreadyToHelp ! Commented Apr 4, 2018 at 15:58
  • 1
    I really hate answers that say "It's an X/Y problem." No, it's a stated problem showing that I attempted to solve it before asking for help. How are people supposed to show what they've done if they don't want comments like this? And I checked for duplicates, but none included the issue with minutes in javascript. I found solutions in PHP, but that doesn't necessary work in javascript. Commented Apr 4, 2018 at 16:10
  • 1
    @kevin everything is somehow slightly related to one of more than the millions of questions that exists on the internet. However its (sometimes) hard to find them, and the askers might not have the same knowledge or skill like you, so they might not be able to adapt the answers to their usecase. If there would be a real dupe, wheres your dupe vote?! Commented Apr 4, 2018 at 16:26
  • 1
    @kevin but you would need to set the start and end date to todays date ... which is slightly longer then the answers proposed below. Commented Apr 4, 2018 at 16:45

3 Answers 3

23

If its 14:04 your condition will fail as 4 is smaller 5. The simplest would probably be to just take the full minutes of the day:

 const start = 13 * 60 + 5;
 const end =  19 * 60 + 57;
 const date = new Date(); 
 const now = date.getHours() * 60 + date.getMinutes();

 if(start <= now && now <= end)
   alert("in time");
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, this is exactly what I was concerned about. Excellent answer.
@alligator glad to help :)
Suggest using, or not using, the UTC methods consistently, rather than mix-and-match. :-)
Apparently two people decided to downvote every answer (a third downvoted one of them). sigh
what if the end is smaller than the start? like from 22 to 03 in the morning?
5

If you're saying you want to check if the time "now" is between two times, you can express those times as minutes-since-midnight (hours * 60 + minutes), and the check is quite straightforward.

For instance, is it between 8:30 a.m. (inclusive) and 5:00 p.m. (exclusive):

var start =  8 * 60 + 30;
var end   = 17 * 60 + 0;

function inTime() {
  var now = new Date();
  var time = now.getHours() * 60 + now.getMinutes();
  return time >= start && time < end;
}

console.log(inTime());

The above uses local time; if you want to check UTC instead, just use the equivalent UTC methods.

1 Comment

If the end date is in the following day, you just need to add 1440 mins to that time. if (end < start) end += 1440;
4

Convert times to milliseconds and then you can compare easily.

short version:

if(timeToCheck.getTime() >= startTime.getTime() &&
        timeToCheck.getTime() <= endTime.getTime()) {
    // ...
}

OR:

let startTimeMilli = startTime.getTime();
let endTimeMilli = endTime.getTime();
let timeToCheckMilli = timeToCheck.getTime();

// change >= to > or <= to < as you need
if (timeToCheckMilli >= startTimeMilli && timeToCheckMilli <= endTimeMilli) {
    // do your things
}

6 Comments

I thought about that, but does that work once, or can I use that to check every single day? For example, if I count up from 1970, I can only compare values within one day - or am I missing something?
check answer again
why are people voting down?
I upvoted not because your answer is right or well written, but because its definetly not worth three downvotes.
Alligator, I did not understand what you are asking. using milliseconds, you don't need to worry about date part. It will work even if start and end times fall on different dates.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.