[英]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 条件进行检查
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.