检查时差是否小于45分钟并运行功能 - AngularJS

moh*_*abk 6 javascript php angularjs

使用这样的代码在PHP中这很容易做到;

if (strtotime($given_time) >= time()+300) echo "You are online";
Run Code Online (Sandbox Code Playgroud)

但是无法在SO上找到任何东西在javascript中做到这一点.我想检查给定时间和当前时间之间的差异是否小于45分钟

例如

$scope.given_time = "14:10:00"
$scope.current_time = new Date();
Run Code Online (Sandbox Code Playgroud)

我只关心时间部分.我需要从中提取时间部分new Date();然后进行比较.

那应该是 true

我怎样才能通过Javascript实现这一目标:

if ($scope.given_time - $scope.current_time < 45 minutes) {
   // do something
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*ete 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");
Run Code Online (Sandbox Code Playgroud)