简体   繁体   English

AngularJS:下拉列表中的月份过滤器

[英]AngularJS: Month filter in drop down list

Below I m looping for month but always feb month is missing;Please help with same.i want to display last 3 month show display. 下面我循环播放一个月,但总是缺少二月;请提供帮助。我想显示最近三个月的显示。

$scope.months = [];
    var myDate = new Date();
    var previousMonth = new Date(myDate);

    for(var i=0;i<4;i++){
    var mon=myDate.getMonth()-i;
    if(mon<-1){
      mon=mon+12;
    }
    previousMonth.setMonth(mon);
    $scope.prevMonth = $filter('date')(previousMonth, 'MMMM');
    console.log("$scope.prevMonth ",$scope.prevMonth)
    $scope.prevYear = $filter('date')(previousMonth, 'yyyy');
    $scope.months.push({"val":previousMonth.getMonth()+1,"month":$scope.prevMonth});
    console.log($scope.months);
    $scope.years.push($scope.prevYear);

    }

Problem: That is because your day is 29 today and in February, max date is 28. when you try to set month as 1 (Feb) and date as 29 which is already set. 问题:这是因为当您尝试将月份设置为1(Feb),将日期设置为已经设置的日期时,今天的今天是2月29日,最大日期是28。 It automatically increases the month ;) 它会自动增加月份;)

For example: 例如:

 var overMaxDate = new Date('2018-03-29'); overMaxDate.setMonth(1) console.log(overMaxDate.toLocaleDateString()); var maxDate = new Date('2018-03-28'); maxDate.setMonth(1) console.log(maxDate.toLocaleDateString()); 

Solution: You can use https://momentjs.com/ library to manipulate date any way you like. 解决方案:您可以使用https://momentjs.com/库来以任意方式操纵日期。 Or you can also keep track of days. 或者,您也可以跟踪日子。 Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM