unsafe_where_true
unsafe_where_true

Reputation: 6300

javascript: evaluate if given hour is between two hours

So there's plenty of examples on how to calculate the time between two dates.

But in my case, I have a date X. Let's say it's today. X has a time associate to it, e.g. 08:00 (Or what I get back from .getHours())

I need to know if the hours of X are between a start hour (say "07:00") and an end hour (say "12:00")

X will be always retrieved via getHours() The start and end hour of the range have a fixed format (e.g. "07:00" and "12:00")

Performance is an issue, so whatever performs better is preferred (e.g. if it implies using moment, that's fine, but if a custom function would perform better, we want that)

My first approach would be, as the formats are fixed, to transform the .getHours() to a number, likewise for the range hours, and then calculate...I feel this approach my have trouble with some special cases I may not be aware of?

Upvotes: 0

Views: 1983

Answers (3)

RobG
RobG

Reputation: 147403

If you want to check part hours, consider converting the hours to minutes, something like the following. How will you deal with ranges that go over midnight? e.g. 23:30 to 01:30.

/*  Determine if the current time is between two provided hours
**  @param {string} h0 - time in format h:mm
**  @param {string} h1 - time in format h:mm
**  @returns {boolean} true if the current time is between or equal to h0 and h1
*/
function betweenHours(h0, h1) {
  var now = new Date();
  var mins = now.getHours()*60 + now.getMinutes();
  return toMins(h0) <= mins && mins <= toMins(h1);
}

/*  Convert hours to minutes
**  @param {string} h - time in format h:mm
**  @returns {number} time converted to minutes
*/
function toMins(h) {
  var b = h.split(':')
  return b[0]*60 + +b[1];
}

 
<form>
  Start time (h:mm)<input name="startHours">
  <br>
  End time (h:mm)<input name="endHours">
  <br>
  <button type="button" onclick="
   this.form.inRange.value = betweenHours(this.form.startHours.value, this.form.endHours.value);
   ">Check range</button>
  <br>
  Currently in range? <input name="inRange" readonly>
</form>
                 

Upvotes: 1

You could use moment-range

From docs:

You can also create a range from an ISO 8601 time interval string:

var timeInterval = "2015-01-17T09:50:04+00:00/2015-04-17T08:29:55+00:00";
var range = moment.range(timeInterval);
range.contains(X); // true if between interval

Upvotes: 1

Brian Glaz
Brian Glaz

Reputation: 15676

Are you dealing with military tine? (From 0:00 to 24:00)

getHours() returns an Integer, and if you are only interested in hours and not minutes, you can use parseInt() to turn the start and end hours into integers as well. For example, parseInt('07:00', 10) will return 7. So if you wanted to create a function to test if the current time is between two hours, it might look something like this:

function isBetweenHours(startHour, endHour)
{
    var now = new Date().getHours();
    return now >= parseInt(startHour, 10) && now <= parseInt(endHour, 10);
}

Then you would use it like this:

if( isBetweenHours('07:00', '12:00') ) { //some code here }

Upvotes: 0

Related Questions