0
$scope.timing = "1:2"; // Ex: "0.3", 10:1", "2:11", "11:41"  

I want the above $scope.timing in 01:02 format.

How can i can achieve this?

7
  • 2
    Moment.js is a great library for everything related to time. Commented Apr 20, 2017 at 14:03
  • So 1:2 would output as 01:02 and not 01:20 Commented Apr 20, 2017 at 14:04
  • 2
    How does 0.3 fit in? Seems out of place Commented Apr 20, 2017 at 14:05
  • @George yes, i want it has 01:02, it automatically takes correct timing when digit is above 10 i.e, 11:41 Commented Apr 20, 2017 at 14:07
  • @charlietfl its like 00:03 mins Commented Apr 20, 2017 at 14:08

1 Answer 1

1

You could use moment.js, as anshulk commented. this also is an opportunity to use a custom filter in the template

<div ng-bind="vm.timing | hhSSFilter"></div>

function hhSSfilter(input) {
  var result = input.split(':').map(d => {
     return d.length < 2 ? '0' + d : d;
  });
  return result.join(':');
}
Sign up to request clarification or add additional context in comments.

2 Comments

fails if you use 11:41
also not a legitmate angular filter construct

Your Answer

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.