简体   繁体   English

如何使用javascript检查范围之间的时间

[英]How to check time between range using javascript

I have the time value in HH:MM format.我有HH:MM格式的时间值。 Hours are in 24 hours format.小时采用 24 小时格式。 eg 14:30例如 14:30

I want to do a checking using two IF condition我想使用两个 IF 条件进行检查

  1. if the time value is between 05:00 to 22:00如果时间值在 05:00 到 22:00 之间
  2. if the time value is between 22:00 to 05:00如果时间值在 22:00 到 05:00 之间

I am not sure how to do that.我不知道该怎么做。

Since times in HH:mm format can be compared directly, you just need to compare the values.由于 HH:mm 格式的时间可以直接比较,您只需要比较这些值。 The following returns true if the time is in range, false otherwise.如果时间在范围内,则以下返回 true,否则返回 false。

 var range = ['05:00','22:00']; function isInRange(value, range) { return value >= range[0] && value <= range[1]; } ['04:59','08:30','23:15'].forEach(function(time) { console.log(time + ' is ' + (isInRange(time, range)? ' ':'not ') + 'in range'); }); // Alternatively ['04:59','23:15','08:30'].forEach(function(time) { var inRange = isInRange(time, range); console.log(time + ' is in range ' + (inRange? range : range.slice().reverse()).join(' - ')); });

Try this code snippet试试这个代码片段

function dateObj(d) {
    var parts = d.split(":"),
        date  = new Date();

    date.setHours(parts[0]);
    date.setMinutes(parts[1]);
    return date;
}
if(dateObj('02:35')>= dateObj('14:30')&& dateObj('14:30')<=dateObj('22:00'))alert('true');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中检查时间范围 - how to check time range in javascript 如何使用 moment.js 检查时间是否在给定范围内? - How to check if the time is in between given range using moment.js? 如何在Javascript中检查选定的时间是否在给定的时间范围内 - How to check selected time is within the given time range in Javascript 如何确定具体时间是否在javascript中的给定时间范围之间 - How to determine if the specific time is between given time range in javascript 如何使用 jquery/javascript 检查当前时间是否在工作日的特定范围内? - How to check if current time falls within a specific range on a week day using jquery/javascript? 如何使用javascript在日期范围之间增加 - how to increment between a range of dates using javascript 如何检查时间是否大于JavaScript中2个日期之间的总时间? - How to check if time is greater than the total time between 2 dates in javascript? 检查Javascript中是否有2次之间的时间 - Check if time between 2 times in Javascript 如何使用 JavaScript 检查网站的响应时间? - How to check the response time of an website using JavaScript? 如何使用jquery / js检查时间范围是否大于7周的时间范围 - How to check if a time range is greater than a 7 week time range using jquery/js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM