2016-06-08 150 views
1

我有這樣的Html。Angular Date Format Not Working

<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> 

輸出是這樣的: 2016年1月6日爲2016-06-30T00:00:00 但我想它是這樣 2016年1月6日爲30-06-2016

第二部分日期格式不工作如何解決?

回答

1

的解決方案是在包( '與過濾器的子表達式')如在(anydate|date:'yyyy')

所以,你的情況:

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

變化

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

http://jsfiddle.net/jsggspm0/

+0

感謝。 khajaamin 是這項工作對我很好。 –

1

可能會解決您的問題,但您必須使用格式化函數在控制器中轉換日期。

請讓我知道是否有任何需要更改的片段正在工作。

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"); 
 
    }; 
 
});
<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>