Question: $scope data convert to date format

Question

$scope data convert to date format

Answers 3
Added at 2017-01-01 08:01
Tags
Question

how to convert $scope data to date format?

{{book.take_time | date: 'HH:mm'}}

$http.get("../api/book.php").then(function(response) {
        $scope.book = response.data;
});

inside book have book_id, book_name, take_time
how to convert take_time to date format?

Answers to

$scope data convert to date format

nr: #1 dodano: 2017-01-01 08:01

Update: The input $scope.book is actually an array of books. Pl refer authors comments.

This is a rather crude way of doing it, but you can try something like this:

<p data-ng-repeat="value in books">    
    {{value.take_time | date: 'shortTime'}} 
</p>

$http.get("../api/book.php").then(function(response) {
      $scope.book = response.data;

      angular.forEach($scope.book, function(value, key) {

        var res = value.take_time.split(":");
        //Assuming time is in hh:mm:ss format
        var date = new Date();
        date.setMinutes(res[1]);
        date.setHours(res[0]);

        value.take_time = date;
      });



});

See a sample of it working here: http://www.w3schools.com/code/tryit.asp?filename=FBB8M1JMPUX5

nr: #2 dodano: 2017-01-01 08:01

According to your comment you can try for "shortTime" date format

'shortTime': equivalent to 'h:mm a' for en_US locale (e.g. 12:05 PM)

for details--https://docs.angularjs.org/api/ng/filter/date

nr: #3 dodano: 2017-01-01 09:01

Use Moment.js to convert time format

moment($scope.data[0].take_time).format('HH:mm')

or if you have array then

angular.forEach($scope.data,function(value,key){value.take_time = moment(value.take_time).format('HH:mm')});

below is link for that js

http://momentjs.com/

Source Show
◀ Wstecz