1

I am trying to compare 2 dates and to filter my data with the result:

my controller:

  $scope.checkDate = function(startDate){
    var todaysDate = new Date();
    return function(coupon){
        return coupon[startDate]<= todaysDate;
    };
}

my html:

  <div class="col-sm-3" id="coupon-tiles" ng-repeat="coupon in allCoupons| filter: checkDate('startDate')" ng-if="coupon.amount>0" >
        <div>
            <image ng-src={{coupon.image}}>
        </div>
        <div>{{coupon.title}}</div>
        <div>price:{{coupon.price}}</div>
        <div>start date:{{coupon.startDate}}</div>
        <div>end date:{{coupon.endDate}}</div>

        <div class="btn-group" role="group" aria-label="...">
            <button type="button" class="btn btn-default" ng-    click="buy(coupon)">Purchase</button>
        </div>

    </div>

The coupons arrive from the db through json, the dates are java sql dates so the format is yyyy,mm,dd , Jersey and Jackson doing the parsing.

Nothing seems to work, can someone help

1
  • 2
    momentjs.com is a good one to play with dates Commented Jun 2, 2016 at 8:38

3 Answers 3

1

Your DB dates are in string format. You need to convert it to JS Date object to compare them. Try the following:

$scope.checkDate = function(startDate){
var todaysDate = new Date();
 return function(coupon){
    return Date.parse(coupon[startDate]) <= todaysDate;
 };
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try to simplify your filter function like

$scope.checkDate = function(startDate){
    return startDate.getTime() <= new Date().getTime();
}

And in your html

 <div class="col-sm-3" id="coupon-tiles" ng-repeat="coupon in allCoupons| filter: checkDate(coupon.startDate)" ng-if="coupon.amount>0" >

1 Comment

startDate.getTime() is un defined, this is the error i get.
0

I am trying this and this is working:

var startDate = $filter('date')($scope.StartDate, "yyyy-MM-dd");
var endDate = $filter('date')($scope.EndDate, "yyyy-MM-dd");
if (startDate > endDate) {
    //some code
}

For using $filter you need to inject $filter in your controller.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.