4

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

1
  • Use ng-if="(product.experationDate < CurrentDate)" instead Commented Feb 10, 2017 at 8:32

3 Answers 3

5

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)"
Sign up to request clarification or add additional context in comments.

6 Comments

Totally agree with that. :)
he is not using moment.. Please answer in context.
Yes that's why I put the comment 'recommend moment.js'
If I would ask a question on SO - i would be glad that people point out better approaches then the ons i'm currently having... +1 @rrd
I've updated the answer to include other possibilities, although he may have to play around with them since I don't know the format of product.experationDate or currentDate
|
3

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.

Comments

1

I don't think you can compare javascript date type directly. Instead, do this and remove {{}}

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

which compares their milliseconds.

3 Comments

i try that but "product.experationDate.getTime()" does not output any thing. why ? however "CurrentDate.getTime()" output somthing like these 15428745
both experationDate and CurrentDate are Date Type
how did you get experationDate? in a promise?

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.