2015-12-04 34 views
5

這是一個很容易做的事,在PHP中用這樣的代碼;檢查時間差是否小於45分鐘,並運行函數 - AngularJS

if (strtotime($given_time) >= time()+300) echo "You are online"; 

但是找不到任何東西在JavaScript中完成此操作。 我要檢查,如果給定的時間和當前時間之間的差小於45分鐘

例如

$scope.given_time = "14:10:00" 
$scope.current_time = new Date(); 

我只關注了一段時間。我需要從new Date();中提取時間部分,然後進行比較。

那麼這應該是true

我怎樣可以用JavaScript實現這一點:

if ($scope.given_time - $scope.current_time < 45 minutes) { 
    // do something 
} 
+1

你可以用'getMinutes()'date對象。 – www139

+1

您是否嘗試過使用像moment.js這樣的插件? –

+1

它多久檢查一次時差?如果你使用了window.setInterval並且每分鐘都檢查一次,你可以使用'getMinutes()'函數並且只是比較它。 – www139

回答

0

試試這個:

function checkTime(time) { 
 
    var date = new Date(); 
 
    var date1 = new Date((date.getMonth() + 1) + "/" + date.getDate() + "/" + date.getFullYear() + " " + time); 
 

 
    var minutes = (date1.getTime() - date.getTime())/(60 * 1000); 
 

 
    if (minutes > 45 || (minutes < 0 && minutes > -1395)) { 
 
    // greater than 45 is todays time is above 45 minutes 
 
    // less than 0 means the next available time will be tomorrow and the greater than -1395 means it will be more than 45 minutes from now into tomorrow 
 

 
    document.write(time + ': true<br />'); 
 
    } else { 
 
    document.write(time + ': false<br />'); 
 
    } 
 
} 
 

 
checkTime("14:10:00"); 
 
checkTime("16:30:00"); 
 
checkTime("17:10:00");

+0

您是否可以修改此功能,以檢查當前時間(包括日期)是否超過給定的時間,請...謝謝 –

+0

@ moh.ABK請你問一個新的問題,因爲它會改變這個相當多 – Pete

2

JavaScript使用毫秒UNIX時間戳,因此它是類似的strtotime的輸出(使用秒)。

var date = new Date(); 

然後你需要從毫秒進行計算。 (Minutes * 60 * 1000)

您也可以使用date.parse()來解析一個字符串到毫秒,就像在0123秒鐘裏用strtotime()來做的那樣。

在全:

var date = new Date(); 
var last = new Date('Previous Date'); // or a previous millisecond timestamp 
if ((date - last) > (45 * 60 * 1000)) { 
    // do something 
} 

你可以使用一個靜態日期比較只是時間,這是的strtotime究竟是幹什麼的,如果你排除日期:

var last = new Date('1/1/70 14:10:00'); 
var date = new Date('1/1/70 14:30:00'); 

但是,這種方法將失敗如果你想比較跨越日界的時間。

+0

我不想考慮'日期'..只需幾分鐘。 '新的日期()'會給你完整的日期字符串 –

+2

@ moh.ABK你不必在你的計算中使用完整的日期字符串,你可以使用一個靜態日期,但你應該。如果時間跨越幾天會發生什麼? 11:59 PM vs 12:01 AM。 – Devon

0

有一個名爲getMinutes()的JavaScript方法;你可以用來獲得只有分鐘和比較。

你的代碼應該是這個樣子:

var received_time = "14:10:00".split(':'); 
    var minute = ''; 
    if(received_time.length === 3) { 
     minute = parseInt(received_time[1], 10); 
    } 
    $scope.given_time = minute; 
    var the_time = new Date(); 
    $scope.current_time = the_time.getMinutes(); 

你現在可以做你的事:

if ($scope.given_time - $scope.current_time < 45 minutes) { 
    // do something 
} 
+1

這不能用來很容易地計算差異。 11:01會返回1和10:59會返回59. – Devon

+0

@Devon,所以...爲什麼不能這樣工作? – Ionut

+1

因爲這將是-58的差異,而不是2 ... – Devon

0

使用像moment.js這樣的庫,你可以簡單地區分兩次。

var $log = $("#log"); 
 

 
/* Difference between just times */ 
 
$log.append("Difference between times\n"); 
 
var givenTime = moment("14:10:00", "HH:mm:ss"); 
 
var minutesPassed = moment("14:30:00", "HH:mm:ss").diff(givenTime, "minutes"); 
 

 
$log.append("Minutes passed: " + minutesPassed + "\n"); 
 

 
if (minutesPassed < 45) { 
 
    $log.append(minutesPassed + " minutes have elapsed. Event Triggered." + "\n"); 
 
} 
 

 

 
/* Better: Difference between times that have dates attached to them and cross a day boundary. */ 
 
$log.append("\n\nDifference between dates with times\n"); 
 
givenTime = moment("2015-12-03 23:33:00"); 
 
minutesPassed = moment("2015-12-04 00:14:00").diff(givenTime, "minutes"); 
 

 
$log.append("Minutes passed: " + minutesPassed + "\n"); 
 

 
if (minutesPassed < 45) { 
 
    $log.append(minutesPassed + " minutes have elapsed. Event Triggered." + "\n"); 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="http://momentjs.com/downloads/moment.js"></script> 
 
<p>Results:</p> 
 
<hr> 
 
<pre id="log"></pre> 
 
<hr>

警告:如果給定的時間是昨天晚上11:30和當前時間是凌晨00:10那麼你會得到錯誤的結果。如果這種類型的事情對您的用例來說是個問題,那麼您會希望在時間上使用日期。

的moment.js文檔

http://momentjs.com/docs/

的時刻文檔角指令

https://github.com/urish/angular-moment/blob/master/README.md

+0

你能更新你的代碼以包含日期嗎?我也可以在Angular中使用正常的「時刻」庫嗎? –

+0

@ moh.ABK我已用日期更新它,但您必須將其更改爲使用日期格式。如果您正在使用angular-moment指令,那麼您應該已經可以在應用程序中使用moment()。你只需要將它注入到你的構造函數中。 – Voziv

相關問題