admin管理员组

文章数量:1399951

I would like to increment the month like this:

 var date = new Date();
    $scope.month = $filter('date')(date, 'MMMM');
    $scope.nextMonth = function () {                
     //set next month
     $scope.month = $filter('date')(new Date($scope.month).getMonth()+1, 'MMMM');
    };

how can i achieve this?

So for example if

$scope.month = "November";

$scope.nextMonth(); should set $scope.month = "December";

NB: I cant use any external library for this

I would like to increment the month like this:

 var date = new Date();
    $scope.month = $filter('date')(date, 'MMMM');
    $scope.nextMonth = function () {                
     //set next month
     $scope.month = $filter('date')(new Date($scope.month).getMonth()+1, 'MMMM');
    };

how can i achieve this?

So for example if

$scope.month = "November";

$scope.nextMonth(); should set $scope.month = "December";

NB: I cant use any external library for this

Share Improve this question edited Sep 8, 2014 at 9:43 Filippo oretti asked Sep 8, 2014 at 9:35 Filippo orettiFilippo oretti 49.9k96 gold badges229 silver badges351 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Angular doesn't have anything built in to do this, so you need to use the native Javascript Date methods.

If you're doing a lot of work with dates, however, I would strongly remend moment.js.

This would make the above incredibly simple:

$scope.month = moment().format('MMMM')
$scope.nextMonth = function(){
    $scope.month.add(1, 'month');
}

EDIT:

Without using an external library you can still do it, and you pretty much have the code:

$scope.nextMonth = function(){
  date.setMonth(date.getMonth() + 1);
  return $scope.month = $filter('date')(date, 'MMMM');
}

works only for months

$scope.nextMonth = function(){
  var index = $locale.DATETIME_FORMATS.MONTH.indexOf($scope.month);
  $scope.month = $locale.DATETIME_FORMATS.MONTH[index == 11 ? 0 : index + 1];
}
 $scope.date = (new Date((new Date()).setMonth((new Date()).getMonth()
 + 1))).toLocaleDateString('fr-FR')

本文标签: javascriptAngular jsincrement months in scopeStack Overflow