2

As the title says, I have a table which one of the columns is an epoch date. I am trying to use javascripts built in 'toISOString()` to translate it, but can't figure out how to do it. IS there a way to do it on the fly while populating the table?

Here is the js for what I am working with:

angular.module('myapp', [])
  .controller('MainCtrl', function($scope, $http, $filter, $interval) {

    var orderBy = $filter('orderBy');
    $scope.savedOrder = 'name';
    $scope.searchText = ''

    //first http request-----------------------------------
    $http.get('xxxxxxxxxxxxxx').success(function(data) {
      $scope.recentalerts = data;
      $scope.tempdata = data;
      $scope.order('-epochtime');
    });

    $scope.reverse = true;

    //beginning of interval--------------------------------
    $interval(function() {
      $http.get('xxxxxxxxxxxxx').success(function(data) {
        if (!angular.equals(data, $scope.tempdata)) { 
          console.log("here...");

          ...

          $scope.tempdata = data; 

        } //end if

      });
    }, 5000);


    $scope.order = function(predicate) {
      $scope.reverse = !$scope.reverse;
      $scope.recentalerts = orderBy($scope.recentalerts, predicate, $scope.reverse);
      $scope.savedOrder = predicate;
    };

  });

And here is the table body :

  <tbody>
    <tr data-ng-repeat="alert in recentalerts | orderBy:savedOrder:reverse | filter:searchText">
      <td ng-click="search(alert.epochtime)">{{alert.epochtime}}</td>
      <td ng-click="search(alert.ip)">{{alert.ip}}</td>
      <td ng-click="search(alert.type)">{{alert.type}}</td>
      <td ng-click="search(alert.classification)">{{alert.classification}}</td>
    </tr>
  </tbody>

I tried {{alert.epochtime.toISOString()}} but that didn't work. I guess it needs to be a date object before I do the toISOString() but is there a way to do that inside the ng-repeat?

erp
  • 2,950
  • 9
  • 45
  • 90
  • 2
    Oh hi! Making progress, I see. I don't know much about angular but perhaps this link will be helpful? http://stackoverflow.com/questions/17925020/angularjs-convert-tag-value-unix-time-to-human-readable-time – Paul May 20 '15 at 16:44
  • 1
    Thanks and yeah, still learning :) – erp May 20 '15 at 18:13

1 Answers1

2

You can use date filter

Date Filter Docs

See the below code.

angular.module('myapp', [])
  .controller('MainCtrl', function($scope) {
    // this will return epoch date
    $scope.epochtime = (new Date).getTime();
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
  <div ng-controller="MainCtrl">
    <div>{{epochtime}}</div>
    <div>{{epochtime | date:'yyyy-MM-dd HH:mm:ss Z'}}</div>
  </div>
</div>
jad-panda
  • 2,509
  • 16
  • 22
  • okay new question. so if I do this `{{alert.epochtime*1000 | date:'yyyy-MM-ddTHH:mm:ss'}}Z` I get what I want but it's in gmt. How could I shave off 4 hrs of that? so for example if the epoch time is is 14:07:33 (2:07:33) when I run it through that formatter it comes out as 10:07:33? Any ideas? – erp May 20 '15 at 17:02
  • you can set timezone. like this `{{ date_expression | date : format : timezone}}` – jad-panda May 20 '15 at 17:04
  • Cool. Thanks for the response. Turns out I just figured what the 'Z' meant. Zulu time is in gmt anyways so there is no need to translate :) – erp May 20 '15 at 17:05