1

I am using angular for developing an hybrid Mobile Application.

I am getting date from Api like this Date : "2017/12/15" but i need to format it into inside expression because i am using ng-repeat to push data in view.

I need to formate it like 15-Dec-2017 or 15-12-2017.

I have tried {{data.Date | date : 'dd-MM-yyyy'}} but this approach is not working.

I followed these link1 , link2 and link3 but none of these resolving my issue.

Community
  • 1
  • 1
Vishal Singh
  • 628
  • 1
  • 7
  • 25

2 Answers2

2

This is not working because your date is in string format.

Convert it to date format like this

$scope.toDate=function(strDt){
 return new Date(strDt);
}

html

{{toDate(data.Date) | date : 'dd-MM-yyyy'}}

DEMO

Anik Islam Abhi
  • 25,137
  • 8
  • 58
  • 80
2

Its type manipulation minor mistake! See following fixed snippet.

angular.module('app', [])
.controller('Ctrl', function($scope) {
  
  $scope.dateObj = function(stringDate) {
    //receives string but returns date object on which filter can be applied
    return new Date(stringDate);
  }

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="Ctrl">
  <div>
    {{ dateObj("2020/12/06") | date : 'dd-MM-yyyy'}}
  </div>
Zeeshan Hassan Memon
  • 8,105
  • 4
  • 43
  • 57