如何从选择下拉列表中的当前日期按名称显示上个月的6个月?

时间:2017-09-21 06:46:57

标签: html angularjs

我正在处理的项目需要在选择下拉列表中按名称显示前6个月,然后将其发送到服务单位。 有没有办法使用JavaScript?

2 个答案:

答案 0 :(得分:0)

你可以做这样的事情

var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var today = new Date();
var d;
var month;

for(var i = 6; i > 0; i -= 1) {
  d = new Date(today.getFullYear(), today.getMonth() - i, 1);
  month = monthNames[d.getMonth()];
  console.log(month);
}

答案 1 :(得分:0)



angular.module('app', [])
.controller('ctrl', ['$scope', function($scope) {
    $scope.data = [];
    var current = moment().month();
    for(var i = 6; i > 0; i--)
      $scope.data.push(moment.months(current - i));
    $scope.selected = $scope.data[0]    
}]);

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://momentjs.com/downloads/moment.min.js"></script>

<div ng-app='app' ng-controller="ctrl">
    <select ng-options="item for item in data" ng-model="selected"></select>
    {{selected}}
</div>
&#13;
&#13;
&#13;