日期比较不能在角度js中工作

时间:2021-04-03 19:37:12

Can anyone tell me why my date is not get working. Basically when i try to compare date then is is not working in angularJs

任何人都可以告诉我为什么我的约会不起作用。基本上当我尝试比较日期时,则不能在angularJs中工作

  var dateObj1 = $filter("date")(Date.now(), 'dd-MMM-yyyy'); // output is "04-May-2016"
    var dateObj2 = $scope.employee.Tue; // output is "03-May-2016"
    if (dateObj1 < dateObj2) {         
        return true
    } else {
        return false;
    }

above is working but for case below is not working if i use date as "26-Apr-2016" i get true in return

以上是工作,但如果我使用日期为“2016年4月26日”,下面的情况不起作用我得到回报

 var dateObj1 = $filter("date")(Date.now(), 'dd-MMM-yyyy'); // output is "04-May-2016"
    var dateObj2 = $scope.employee.Tue; // output is "26-Apr-2016"
    if (dateObj1 < dateObj2) {         
        return true
    } else {
        return false;
    }

2 个解决方案

#1


2  

According to the documentation of the date filter, this filter

根据日期过滤器的文档,此过滤器

Formats date to a string based on the requested format.

根据请求的格式将字符串格式化为字符串。

So when comparing dateObj1 to dateObj2, you are using the string comparison which is the lexicographic order.

因此,在将dateObj1与dateObj2进行比较时,您使用的是字符串比较,即字典顺序。

You must parse your string to a Date (by using Date.parse) to obtains the wanted results

您必须将字符串解析为Date(通过使用Date.parse)以获取所需结果

#2


1  

Check out This, Date.parse is needed to accomplish this task

看看这个,Date.parse是完成这个任务所必需的

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

jimApp.controller('mainCtrl', function($scope, $filter){
  var dateObj1 = $filter("date")(Date.now(), 'dd-MMM-yyyy'); // output is "04-May-2016"
  var dateObj2 = Date.parse("26-Apr-2016");
  if (Date.parse(dateObj1) < dateObj2) {
    alert(true);
    return true
  }
  else {
    alert(false);
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainCtrl">
</div>

#1


2  

According to the documentation of the date filter, this filter

根据日期过滤器的文档,此过滤器

Formats date to a string based on the requested format.

根据请求的格式将字符串格式化为字符串。

So when comparing dateObj1 to dateObj2, you are using the string comparison which is the lexicographic order.

因此,在将dateObj1与dateObj2进行比较时,您使用的是字符串比较,即字典顺序。

You must parse your string to a Date (by using Date.parse) to obtains the wanted results

您必须将字符串解析为Date(通过使用Date.parse)以获取所需结果

#2


1  

Check out This, Date.parse is needed to accomplish this task

看看这个,Date.parse是完成这个任务所必需的

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

jimApp.controller('mainCtrl', function($scope, $filter){
  var dateObj1 = $filter("date")(Date.now(), 'dd-MMM-yyyy'); // output is "04-May-2016"
  var dateObj2 = Date.parse("26-Apr-2016");
  if (Date.parse(dateObj1) < dateObj2) {
    alert(true);
    return true
  }
  else {
    alert(false);
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainCtrl">
</div>