角度日期格式不起作用

时间:2016-06-08 08:24:59

标签: angularjs

我有这样的Html。

<select name="paymentList" class="form-control" tabindex=""
                        ng-focus="true"
                        ng-disabled="!showRoomPaymentList"
                        move-focus-by-enter-key
                        ng-model="paymentList"
                        ng-options="s.FromDate | date: 'dd-MM-yyyy' + ' To ' + s.ToDate|date:'dd-MM-yyyy'  for s in showRoomPaymentList track by s.PaymentDurationId"
                        ng-required="true">
                    <option value="">--Select Payment Duration--</option>
                </select>

这样的输出: 01-06-2016至2016-06-30T00:00:00 但我想要这样 01-06-2016至30-06-2016

第二部分日期格式无法解决?

2 个答案:

答案 0 :(得分:1)

可能这解决了您的问题,但您必须使用格式功能在控制器中转换日期。

如果需要更改代码片段,请告知我们。

&#13;
&#13;
var app = angular.module('app', []);


app.controller('Test', function($scope,$filter) {

  $scope.selected = null;
  $scope.items = [{
    name: 'a',
    value: 1,
    something: "xyz",
from:"2016-06-30T00:00:00",
to:"2016-06-30T00:00:00"
  }, {
    name: 'b',
    value: 2,
    something: "xyz",
from:"2016-06-30T00:00:00",
to:"2016-06-30T00:00:00"

  }, {
    name: 'c',
    value: 3,
    something: "xyz",
from:"2016-06-30T00:00:00",
to:"2016-06-30T00:00:00"

  }]


  $scope.show = function() {
    alert("selectec " + $scope.selected.name + ' with value ' + $scope.selected.value);
  }

  $scope.format = function(i) {
    return  $filter('date')(i.from, "dd/MM/yyyy") + " to "+$filter('date')(i.to, "dd/MM/yyyy");
  };
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<html ng-app="app">

<body>
  <div ng-controller="Test">
    <select data-ng-options="i as format(i) for i in items" ng-model="selected">
    </select>
    <button ng-click="show()">press</button>
  </div>
</body>

</html>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

解决方案是在{(1}}中打包()中的'带有过滤器的子表达式'。

所以,在你的情况下:

(anydate|date:'yyyy')

更改为

s.FromDate | date: 'dd-MM-yyyy' + ' To ' + s.ToDate|date:'dd-MM-yyyy' 

请参阅http://jsfiddle.net/jsggspm0/