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";
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 badgesNB: I cant use any external library for this
3 Answers
Reset to default 5Angular 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
版权声明:本文标题:javascript - Angular js - increment months in $scope - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744213774a2595542.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论