Abul Hasan
Abul Hasan

Reputation: 53

How to filter date in angular from current date?

I have date filter. I need to filter date list ago current date and date list before current date.

I used $scope.date = new Date(); $scope.ago = new Date() < $scope.date; $scope.before = new Date() > $scope.date;

but does not works. This is my code:

script js:

var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope,$http) {

   $scope.date = new Date();


    $http.post("getProject.php")
    .success(function (response) {$scope.projects = response;});

var now = new Date().getTime();
   $scope.date = new Date(2015, 10, 10);
   $scope.ago = now < $scope.date.getTime();
   $scope.before = now > $scope.date.getTime();


})

php code:

<?php
    include_once 'dbcon.php';
?>

<!DOCTYPE html>

<html ng-app="myApp">

<head>   

<script src="js/angular.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="js/app.js"></script>

</head>

<body ng-controller="myCtrl">  
    <p>Filtering input:</p>  

      <p><select ng-model="sdate">
    <option value="">Search by title</option>
        <option value="{{ago}}">Current Deals</option>
        <option value="{{before}}">Past Deals</option>
    </select></p>
    <table>

<thead>
        <tr>
            <th>Title</th>
            <th>Start Date</th>
            <th>End Date</th>
            <th>Promotion</th>
        </tr>
</thead>

<tbody>

<tr ng-repeat="project in projects | filter:sdate | orderBy:'title'">

<td>{{project.title}}</td>
<td>{{project.startdate}}</td>
<td>{{project.enddate}}</td>
<td>{{project.promotion}}</td>

</tr>

</tbody> </table>
</body>

</html>

When current Date will select it will show all date list before the current Deals and when select Past Deals it will show ago current Date. I tried AGE's code but it does not work. How can I solve this? Thanks in Advance.

Upvotes: 0

Views: 2471

Answers (2)

Emir Marques
Emir Marques

Reputation: 2687

Use getTime for compare date as:

function ControllerApp($scope){
   var now = new Date().getTime();
   $scope.date = new Date(2015, 10, 10);
   $scope.ago = now < $scope.date.getTime();
   $scope.before = now > $scope.date.getTime();
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="ControllerApp">
  Ago: {{ago}}
  Before: {{before}}
</div>

Upvotes: 1

ari
ari

Reputation: 36

I'm not sure I understand your question correctly. But if you are trying to find whether a date is before today or after today, you can easily achieve that using moment.js. Javascript default Date object is not very handy for comparison. http://momentjs.com/docs/#/query/is-before/ var now = moment(); var beforeThisMoment = false; var afterThisMoment = false; if(now.isBefore(someDate)) { afterThisMoment = true; } else { beforeThisMoment = true; }

Upvotes: 0

Related Questions