prashant.fepale
prashant.fepale

Reputation: 577

date filter is not working with datetime in angularJS

I am using date filter to format my date. but it works fine for date only. But when I go with date time its not working:

<tr class='clickable-row' ng-repeat="exam in examlist" ng-click="gotoProfile(exam.id)">
<td class="custom-icon-size" style="border-left: 0px solid #e0e0e0;">{{ $index + 1 }}</td>
<td class="custom-icon-size text-left">{{ exam.examtitle }}</td>
<td class="custom-icon-size">{{ exam.batchtitle }}</td>
<td class="custom-icon-size">{{ exam.standard }}</td>
<td class="custom-icon-size">{{ exam.examdate | date:"dd MMMM , yyyy" }}</td>
<td class="custom-icon-size">{{ exam.totalmarks }}</td>
<td class="custom-icon-size">{{ exam.status }}</td>
<td style="border-right: 0px solid #e0e0e0;">{{ exam.lastupdate | date:"dd MMM , yyyy hh:mm a" }}</td>

this {{ exam.examdate | date:"dd MMMM , yyyy" }} currently but {{ exam.lastupdate | date:"dd MMM , yyyy hh:mm a" }} it remain the same. like 2017-03-16 12:02:15

Thanks

Upvotes: 0

Views: 1320

Answers (1)

Sachila Ranawaka
Sachila Ranawaka

Reputation: 41387

If the date is string you need to convert it to date object. create a function to convert string date to date

 $scope.formatDate = function(date){
      return new Date(date)
 }

{{ formatDate(lastupdate) | date:"dd MMM , yyyy hh:mm a" }}

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.lastupdate = "2017-03-16 12:02:15";

$scope.formatDate = function(date){
  return new Date(date)
}
})

 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 {{ formatDate(lastupdate)  | date:"dd MMM , yyyy hh:mm a" }}
    </div>

Upvotes: 1

Related Questions