1

I want to show a div of form fields in a form (containing reason for rush service) if the value of Due_Date is a date less than or equal to when the form is completed.

I have tried ng-if but it waits until the form is submitted to work and I tried to figure out a ng-show or ng-hide but dates are making it impossible. Do I need to create a new Date object in javascript to compare it to or does AngularJS know what time it is? What format do I need to convert to for date subtraction?

Any guidance is coveted.

Tim
  • 41,901
  • 18
  • 127
  • 145
jgravois
  • 2,559
  • 8
  • 44
  • 65
  • 1
    Please, post your code and create a fiddle, so it becomes easier for us to help. – Beterraba May 07 '14 at 13:51
  • 1
    You might be able to get away with some shorthand inline, but it's easier to maintain if you create a scope variable –  May 07 '14 at 14:12

1 Answers1

3

In javascript, you need to get today, 14 days forward, and the date you're using:

$scope.aDate= new Date(); // here this is today, but use whatever date you want
$scope.today= new Date();
$scope.twoWeeksForward = new Date();
$scope.twoWeeksForward.setDate($scope.twoWeeksForward.getDate() + 14); // plus 14 days

So in AngularJS, inline HTML:

<div ng-show="today < aDate && aDate < twoWeeksForward"> stuff </div>
<!-- you can probably remove "today < aDate", that just checks if the date hasn't already passed -->

Helpful link:

Compare two dates with JavaScript

Community
  • 1
  • 1
SoluableNonagon
  • 11,541
  • 11
  • 53
  • 98