3

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?

Rob
15.2k30 gold badges49 silver badges73 bronze badges
asked Jan 1, 2017 at 7:11
5
  • What is take time currently? is an Unix timestamp? Commented Jan 1, 2017 at 7:13
  • now string format like this 03:25:30. Commented Jan 1, 2017 at 7:25
  • If that is the format, you have an already converted time. It seems to me you want to skip the seconds, and only keep the hours and minutes. You could do a substring in order to fix that (if all your times are formatted the same), e.g. var converted = book.take_time.substring(0,6) Commented Jan 1, 2017 at 7:40
  • If i have convert to this format 3:25 AM how can i do? Commented Jan 1, 2017 at 7:42
  • date filter only works with date type objects. Please see documentation. Try passing the original date object to the filter instead. Commented Jan 1, 2017 at 7:54

3 Answers 3

1

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

answered Jan 1, 2017 at 7:57
Sign up to request clarification or add additional context in comments.

4 Comments

[{"book_id":"161","book_name":"C++","take_time":"13.50.43"},{"book_id":"162","book_name":"Ruby","take_time":"14.27.02"}]
my scope book will be like this
change the script accordingly then..include a for loop. The above ans works assuming $scope.book is something like this {"book_id":"161","book_name":"C++","take_time":"13.50.43"}
1

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/

DavidPostill
7,9799 gold badges45 silver badges70 bronze badges
answered Jan 1, 2017 at 8:57

Comments

0

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

answered Jan 1, 2017 at 7:57

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.