日期比较Angular JS

时间:2018-07-24 06:48:35

标签: javascript angularjs

var discover = $filter("date")(new Date('08-24-2015'), "MM/dd/yyyy");
var occur = $filter("date")(new Date('06-07-2018'), "MM/dd/yyyy")
console.log(discover);
console.log(occur);
if (discover < occur) {
    console.log('Discover less than occur');
} else {
    console.log('No');
}

对于我分配给发现变量的任何值,结果总是

我比较错了吗?

2 个答案:

答案 0 :(得分:1)

我建议您将momentJS用于日期时间操作。实施起来更清洁。

要检查某个时间是在另一时间之前还是之后,您可以使用他们的query functions

moment('2010-10-20').isBefore('2010-12-31', 'year'); // false
moment('2010-10-20').isBefore('2011-01-01', 'year'); // true

答案 1 :(得分:0)

// Code goes here

var app = angular.module("app", []);

app.controller("BaseController", function($scope,$filter) { 
  
  var discover = $filter("date")(new Date('05-24-2015'), "MM/dd/yyyy");
  var occur = $filter("date")(new Date('06-07-2018'), "MM/dd/yyyy")
  console.log(discover);
  console.log(occur);
  if (discover < occur) {
      console.log('Discover less than occur');
  } else {
      console.log('No');
  }
   
}); 
<!DOCTYPE html>
<html>

  <head>
    <script data-require="angularjs@1.5.5" data-semver="1.5.5" src="https://code.angularjs.org/1.5.5/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <body ng-app="app" ng-controller="BaseController">
 
  </body>

</html>