2017-08-29 148 views
0

我正在尋找一種方法來創建一個腳本,該腳本在某個時間範圍內返回true。比如我有下一次範圍:確定一個時間表是否在時間範圍內 - AngularJS

週一至週五上午8:00以來至11:00

如果「現在」是星期一在15:00返回false。但是如果「現在」是星期二的9:00,則返回true。

我該如何與AngularJS做到這一點?

回答

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));

希望這有助於您。

相關問題