Reputation: 49817
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
Upvotes: 3
Views: 6238
Reputation: 39
$scope.date = (new Date((new Date()).setMonth((new Date()).getMonth()
+ 1))).toLocaleDateString('fr-FR')
Upvotes: 0
Reputation: 862
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];
}
Upvotes: 1
Reputation: 19098
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 recommend 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');
}
Upvotes: 5