2016-04-26 95 views
0

我正在使用角度日期選擇器。當用戶選擇一個日期時,我想過濾結果。如何在角度引導日期選擇器中轉換日期格式

我已經將ng-change添加到日期選擇器,它正在檢測更改並傳遞日期值。但格式不正確。

<uib-datepicker ng-model="dt" ng-change="dateSelected('{{dt}}')" class="well well-sm" datepicker-options="options"></uib-datepicker> 

控制器

$scope.dateSelected = function(passedDate){ 
    console.log('Date Selected ' + passedDate); 
} 

這給

2016-04-26T12:18:56.794Z 

如何將其轉換爲

18 March, 2016 

或類似的東西。

通過基於文檔更新$ scope.options,我嘗試了幾個選項,但沒有任何效果。

回答

1

試試這個:

的HTML應該是這樣的,你不需要{{}}內納克 - 改變

<uib-datepicker ng-model="dt" ng-change="dateSelected(dt)" class="well well-sm" datepicker-options="options"></uib-datepicker> 

var monthArray = ["January", "February", "March", "April",....]; 

和JS壽LD像

console.log($scope.dt.getDate()+', '+monthArray[$scope.dt.getMonth()]+', '+$scope.dt.getFullYear()); 
+0

這是行不通的,當我用passedDate替換newDate時,它給出錯誤passedDate.getMonth不是函數 –

+0

編輯我的答案嘗試使用$ scope.dt。 getMonth,而不是使用passedDate.getMonth –

+0

是的,它的工作現在,謝謝 –

0

日期選擇器在HTML

<div class="input-group ui-datepicker"> 
           <input type="text" 
             class="form-control date" 
             datepicker-popup="{{format}}" 
             ng-model="dt" 
             is-open="opened" 
             min-date="minDate" 
             max="'2015-06-22'" 
             ng-readonly="true" 
             datepicker-options="dateOptions" 
             date-disabled="disabled(date, mode)" 
             ng-required="true" 
             close-text="Close" style="cursor:auto;"> 
           <span class="input-group-addon" ng-click="open($event)"><i class="fa fa-calendar"></i></span> 
          </div> 

設置的格式爲:

$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate']; 
    $scope.format = $scope.formats[0]; 

$scope.dateOptions = { 
     formatYear: 'yy', 
     startingDay: 1 
    }; 
+0

HTML是這樣的 -

+0

不管HTML如何使用,日期格式都是相似的。你有相同的模型和方法來改變格式 – Sajal

相關問題