javascript: evaluate if given hour is between two hours

别来无恙 提交于 2020-01-06 20:04:03

问题


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?


回答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



回答2:


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>
                 



回答3:


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 }


来源:https://stackoverflow.com/questions/35090969/javascript-evaluate-if-given-hour-is-between-two-hours

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!