9

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/…
    – dnp1204
    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.
    – Alligator
    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

20

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");
5
  • Yes, this is exactly what I was concerned about. Excellent answer.
    – Alligator
    Commented Apr 4, 2018 at 15:54
  • @alligator glad to help :) Commented Apr 4, 2018 at 15:54
  • Suggest using, or not using, the UTC methods consistently, rather than mix-and-match. :-) Commented Apr 4, 2018 at 15:56
  • 3
    Apparently two people decided to downvote every answer (a third downvoted one of them). sigh Commented Apr 4, 2018 at 15:58
  • what if the end is smaller than the start? like from 22 to 03 in the morning? Commented Apr 8, 2021 at 17:21
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
  • 1
    If the end date is in the following day, you just need to add 1440 mins to that time. if (end < start) end += 1440;
    – luenib
    Commented Sep 16, 2021 at 19:19
2

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
  • 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?
    – Alligator
    Commented Apr 4, 2018 at 15:48
  • check answer again
    – Tuhin Paul
    Commented Apr 4, 2018 at 15:54
  • why are people voting down?
    – Tuhin Paul
    Commented Apr 4, 2018 at 15:57
  • 1
    I upvoted not because your answer is right or well written, but because its definetly not worth three downvotes. Commented Apr 4, 2018 at 16:00
  • 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.
    – Tuhin Paul
    Commented Apr 4, 2018 at 16:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.