Search code examples
javascriptangularjsdatedatepickerangular-bootstrap

How to convert date format in angular bootstrap date picker


I am using angular date picker. When user selects a date, I want to filter results.

I've added ng-change to the date picker and it is detecting a change and passing the date value. But the format is not correct.

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

Controller

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

This gives

2016-04-26T12:18:56.794Z

How do I convert it to

18 March, 2016

or something similar.

I'tried a couple of option by updating $scope.options based on the documentation, but nothing works.


Solution

  • Try this:

    The html should be like this you don't need {{}} inside ng-change

    <uib-datepicker ng-model="dt" ng-change="dateSelected(dt)" class="well well-sm" datepicker-options="options"></uib-datepicker>
    
    var monthArray = ["January", "February", "March", "April",....];
    

    And JS should be like

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