0

How can I modify this angular function to retrieve range of dates. For now its looping 20 times. How can I only retrieve date from Date().getFullYear(); --> Which is current year till 2020. So in range it should display 2017, 2018, 2019, 2020.

$scope.myfunction = function () {
    var year = new Date().getFullYear();
    var range = [];
    range.push(year);
    for (var i = 1; i < 20; i++) {
        range.push(year + i);
    }
    return range;
}


<select ng-options="year for year in myfunction()" name="year" ng-model="year" required>
    <option value selected disabled>select year</option>
</select>

1 Answer 1

1

You can do something like this. You can set the end year or pass to the function as an argument.

$scope.myfunction = function (endYear) {
      var range = [];
      var startYear = new Date().getFullYear();
      endYear = endYear || 2020;

      while ( startYear <= endYear ) {
             range.push(startYear++);
      } 

      return range;

}

Sign up to request clarification or add additional context in comments.

3 Comments

what if i dont pass anything here select ng-options="year for year in myfunction()" I mean inside function
would it pick 2020 as default
Yes it should pickup 2020 if you don't pass anything. Also if 2020 is a fixed date and not need to change you can remove the argument on the function altogether.

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.