Take the result of ng-repeat and use in Moment.js

Asked

Viewed 215 times

2

I have an app that uses a ng-repeat to list information. One of this information comes with a date as an example below, what happens is that I wanted to use the relative function of moment.Js. My difficulty is to take this received value and use in the right parameters of moment.js.

<tbody>
    <tr ng-repeat="x in names">
        <td>
            {{ x.TempoEspera }} 
        </td> 
    </tr>                                               
</tbody>

received value= "Time":"11/13/2014 17:17:47"

  • 1

    leozado you want to format your date in a specific format?

  • yes, I would like it not to return this date 'RAW' but in this following format for example. 1 minutes ago or 10 hours ago.

  • You want to show the elapsed time of the informed date so far?

1 answer

2


You can make a filter that receives the date and format as you wish:

angular.module("app", []);

angular.module('app').controller("myctrl", function() {
  
  var ctrl = this;
  ctrl.names = [{tempoEspera: "2015-01-01T01:00:00"}, {tempoEspera: "2016-01-01T23:00:59"}]

});

// cria um filtro que receba uma data e formata-a atraves do momentjs
angular.module('app').filter('formatarData', function() {
  return function(input) {

    if (!input) {
      return '';
    }

    return moment(input).format("LLLL"); // formata conforme a localização do utilizador. Alterar esta linha para alterar a forma como a data e formatada.
  }
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.11.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.0/angular.min.js"></script>

<div ng-app="app" ng-controller="myctrl as ctrl">
  <table class="table table-bordered table-striped">
    <thead>
      <tr>
        <th>Test</th>
      </tr>
    </thead>
    <tbody>
      <tr ng-repeat="x in ctrl.names">
        <!--A cada iteração do ng-repeat, o filtro formatarData e aplicado ao conteudo da variavel x.TempoEspera -->
        <td>{{x.tempoEspera | formatarData}}</td>
      </tr>
    </tbody>
  </table>
</div>

In the example above, a filter called formatarData which receives a string representing a date and formats it using the momentjs.

In this specific case, format the date in the format LLLL - Month name, day of Month, day of week, year, time.

However, you can change the filter code to format the date to whatever format you need.

  • right, but then Voce gets the value of a variable and how I could filter the value that comes from another place that in the case was declared that way: var app = angular.module('myapp', []); app.controller('customersCtrl2', Function($Scope, $http, $Ctrl) { $http.get("http://192.168. 0.235:8037/webrun/Fluxoweb002.Rule? sys=EXP") . then(Function (Response) {$Scope.Names = Response.data.requests;}); });

  • The value {Tempoespera: "2000-01-01T01:00:00"} comes from this page "http://192.168. 0.235:8037/webrun/Fluxoweb002.Rule? sys=EXP", along with other information, and I would only need to format the Time value.

  • I don’t know if I understand, the way you show it, it’s been working since $scope.names has objects that in turn have the property TempoEspera.

  • When other information comes that is not date it returns that { Invalid date}

  • Wow I was so focused that the tests I was doing ended.

  • was returning invalid date because I kept the filters on the other calls in the tests I was doing through your code, but when I use the filter in my application it does not work properly would have how to help me fix the filter so it works from that code var app = angular.module('myapp', []); app.controller('customersCtrl2', Function($Scope, $http, $Ctrl) { $http.get("192.168.0.235:8037/webrun/Fluxoweb002.Rule? sys=EXP") . then(Function (Response) {$Scope.Names = Response.data.;}); });

  • you must register the filter before you can use it in HTML, see the line angular.module('app').filter('formatarData', function() of the answer code.

  • 1

    I did, thank you very much

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.