1

In my project the data is coming to front-end as a json object as shown below:

{
  id: 1,
  meetingName: "Meeting 1",
  meetingDate: "2018-02-21",
  startTime: "10:00:00"
}

<td>{{meeting.startTime|date:"h:mma"}}</td>

I used the above method to format the date in angularjs code as 10:00 AM. But the start time is still shown as 10:00:00. Why is it not formatting the date according to the format?

4
  • 2
    it's a string and not a Date object Commented Mar 5, 2018 at 9:57
  • 2
    because the startTime you are getting in response is in string format and not in date format. Commented Mar 5, 2018 at 10:04
  • Try to convert your string date into long format. Then use a date filter. Commented Mar 5, 2018 at 10:13
  • Yes it has happened since the start time is not a Data object. it should be a format of Date object (eg: startTime: "2018-03-05T10:22:24.361Z") to format using angularjs date formats. Thank you for your answers. Commented Mar 5, 2018 at 10:31

2 Answers 2

2

date filter expects a date object as input. But you are passing a string. Below is a sample code that show the date as expected.

var app = angular.module('myApp', []);
app.controller('datCtrl', function($scope) {
  let info = {
    id: 1,
    meetingName: "Meeting 1",
    meetingDate: "2018-02-21",
    startTime: "10:00:00"
  }
  $scope.meetingDate= new Date(info.meetingDate + " " + info.startTime);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="datCtrl">

    <p>Meeting Time= {{ meetingDate | date:"h:mma" }}</p>

  </div>

</body>

Date Filter Docs

Hope this helps :)

Sign up to request clarification or add additional context in comments.

Comments

0

Filter date expects an object of type date. This custom filter could help you:

View

<div ng-controller="MyCtrl">
  {{data.startTime|timeFilter: data.meetingDate: 'h:mma'}}
  {{data.startTime|timeFilter: data.meetingDate: 'yyyy-MM-dd HH:mm:ss Z'}}
</div>

AngularJS application

var myApp = angular.module('myApp', []);

myApp.controller('MyCtrl', function($scope) {
  $scope.data = {
    id: 1,
    meetingName: "Meeting 1",
    meetingDate: "2018-02-21",
    startTime: "10:00:00"
  };
});

myApp.filter('timeFilter', function ($filter) {
  return function (data, aDate, dateFilter) {
    return $filter('date')(new Date(aDate + " " + data), dateFilter);
  }
})

> Demo fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.