messif
messif

Reputation: 92

Datetime won't filter into angular mediumTime.

I have looked through several questions on SO to try to format datetime data pulling into json to no avail. I need to pull in a format that can be read by another function that changes a cell to red after a 5 minute time out. How would I go about fixing the code to display a correct format and creating the function to handle the timeout of the cells? The data does update from the database just fine.

Here is my code:

html:

<form data-ng-submit="submit()" data-ng-controller="Ctrl">
            <p data-ng-repeat="beat in beats">
                Station ID: {{ beat.stationID }}
                Uptime: {{ beat.lastPinged | date : 'mediumTime'}}
            </p>
            <input type="submit" id="submit" value="Submit" />
        </form>

js:

function Ctrl($scope) {
        $scope.submit = function () {
            //Make sure to change the host and port to match the URL 
            var query = "http://localhost:?????/RESTService.svc/ReadAllHeartbeats?";
            $.ajax({ url: query, crossDomain: true, dataType: 'json', processData: true, type: 'GET' })
                .done(function (json) {
                    $scope.beats = json;
                    $scope.$apply();
                })
                .fail(function () {
                alert("Read All Error");
                });
        }
    }

Example of output:

Station ID: test Uptime: /Date(1405628374550-0400)/

Upvotes: 0

Views: 776

Answers (1)

sylwester
sylwester

Reputation: 16498

For json data you can create your own filter : sample here : http://jsbin.com/taqiq/2/edit

app.filter('mydate' ,function($filter){

  return function(input){input =input.replace(/\//g, '');

     var myDate = new Date(input.match(/\d+/)[0] * 1);    


    return $filter('date')(myDate, 'mediumTime');


  };

});

and in html

<form data-ng-submit="submit()" data-ng-controller="Ctrl">
            <p data-ng-repeat="beat in beats">
                Station ID: {{ beat.stationID }}
                Uptime: {{ beat.lastPinged | mydate}}
            </p>
            <input type="submit" id="submit" value="Submit" />
        </form>

Upvotes: 1

Related Questions