Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare dates within ng-if angular

Tags:

html

angularjs

I am looking to compare two dates in ng-if this is what my jade file looks like.

html :

<button ng-if="{{product.experationDate}} < {{CurrentDate}}" type="button> 
// do somthing
</button>

javascript :

$scope.CurrentDate = new Date();

and : date_expiration: {type: Date}

but it not work ! any help will be appreciated. thanks

like image 444
Nassim Avatar asked Oct 24 '25 04:10

Nassim


2 Answers

Don't do this kind of logic in the template. That's what your services and controllers are for. You need to have a method in your controller that returns the boolean:

isExpirationExpired(product) {
  // your date logic here, recommend moment.js;
  return moment(product.experationDate).isBefore(moment(currentdate));
  // or without using moment.js:
  return product.experationDate.getTime() < currentdate.getTime();
  // or using Date
  return new Date(product.experationDate).valueOf() < new Date(currentdate).valueOf();
}

And your template:

ng-if="vm.isExpirationExpired(product)"
like image 94
rrd Avatar answered Oct 26 '25 17:10

rrd


You can try with:

<button ng-if="product.experationDate < CurrentDate" type="button> // do somthing </button>

In Angular directives like ng-if, you don't need the {{ }} as the expression is evaluate either way.

like image 23
anthares Avatar answered Oct 26 '25 19:10

anthares



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!