Vishal Singh
Vishal Singh

Reputation: 628

angularjs : format API return date into dd-mm-yyyy

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.

Upvotes: 1

Views: 1131

Answers (2)

Zeeshan Hassan Memon
Zeeshan Hassan Memon

Reputation: 8325

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>

Upvotes: 2

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

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

Upvotes: 2

Related Questions