我正在寻找一种方法来创建一个脚本,如果值在一个时间范围内,则返回true。例如,我有下一个时间范围:
周一至周五8:00至11:00
如果“now”是星期一15:00返回false。但如果“现在”是星期二9点返回true。
如何使用AngularJS进行操作?
答案 0 :(得分:0)
检查出来:
// The date variable will be the date we
// want to check if it's in the time range
var date = new Date('08-30-2017 09:00:00');
// We define the start and the end of the
// time range
var start = new Date('08-29-2017 09:00:00');
var end = new Date('08-31-2017 17:00:00');
// And create a Date type prototype function
// that will return if our date is inside that
// time range
Date.prototype.isInTimeRange = function(now, end) {
// We run the getTime() method to convert the date
// into integers
return (this.getTime() >= start.getTime() && this.getTime() <= end.getTime());
}
// This should return true - the date
// is inside the time range
console.log(date.isInTimeRange(start, end));
日期不在时间范围内:
// The date variable will be the date we
// want to check if it's in the time range
var date = new Date('08-27-2017 09:00:00');
// We define the start and the end of the
// time range and again
var start = new Date('08-29-2017 09:00:00');
var end = new Date('08-31-2017 17:00:00');
// And create a Date type prototype function
// that will return if our date is inside that
// time range
Date.prototype.isInTimeRange = function(now, end) {
// We run the getTime() method to convert the date
// into integers
return (this.getTime() >= start.getTime() && this.getTime() <= end.getTime());
}
// This should return false - the date
// is not inside the time range
console.log(date.isInTimeRange(start, end));
希望这会对你有所帮助。