-1

I have a Time being pulled from a server in the following JSON format:

"begins_at":"2016-04-22T08:11:07.000Z"

I am using momentjs with angular with the following filter:

{{event_details.begins_at | moment: "format": "HH:MM a"}}

I would expect the following output: 08:11 am. Instead, I am getting: 04:04 am. I suspect it might have something to do with timezones, but I'm not sure. How do I fix this?

Philip7899
  • 4,599
  • 4
  • 55
  • 114
  • 1
    moment is a js lib, not an angular filter, if you have defined a filter named `moment` that utilizes that lib then post it's code – svarog Apr 22 '16 at 15:19
  • actually angular-moment comes with built-in filters. I didn't define anything. – Philip7899 Apr 28 '16 at 14:23

2 Answers2

0

Change MM to mm (months to minutes : ))

angular.module('app', [])
  .provider('moment', function() {  // be nice for DI
    this.$get = function() {
      return moment
    }
  })
  .controller('ctrl', function() {
    this.event_details = {
      begins_at: '2016-04-22T08:11:07.000Z'
    }
  })
  .filter('moment', function(moment) {
    return function(input, options) {
      return moment(input).format(options.split('\'')[1]) // ugly formatter ; (
    }
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app='app'>
  <div ng-controller='ctrl as c'>
    moment: {{ c.event_details.begins_at | moment:"format: 'HH:mm a'" }}
  </div>
</div>
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
0

An alternative. JS date is an ISO Date so we just need to use moments conversion method to use JS native date formatting. This does not require you to create a custom formatter.

{{myMomentDate.toISOString() | date: <your angular filter>}}
Kentonbmax
  • 938
  • 1
  • 10
  • 16