5

How can I reformat time 06:31:04 to display only hh:mm - 06:31

Have tried

$scope.date = '06:31:04';

<p ng-bind="date | date:'HH:mm'"></p>

but time not formatting

How should I make it, thanks

3 Answers 3

8

The best solution was to write a filter

angular.module('foo', [])
.filter('formatTime', function ($filter) {
return function (time, format) {
    var parts = time.split(':');
    var date = new Date(0, 0, 0, parts[0], parts[1], parts[2]);
    return $filter('date')(date, format || 'h:mm');
};
});




{{ '06:31:04' | formatTime }}
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfect to me, but in return $filter('date')(date, 'h:mm') I turn for return $filter('date')(date, 'h:mm a')
7

See the documentation.

<p ng-bind="date | date:"hh:mm"}}></p>

Besides I think the format of your date is wrong. Because

JavaScript Date instance [...] represents a single moment in time. Date objects are based on a time value that is the number of milliseconds since 1 January, 1970 UTC. (source)

Example: 1288323623006. So your format is not recognizable for the filter. Try:

$scope.date = new Date();

If you want to convert a string in your given format to a date, try:

var dateElements = "06:31:04".split(':');
var date = new Date();
date.setHours(time[0]);
date.setMinutes(time[1]);
$scope.date = date;

Then you can format it with the filter.

2 Comments

have applied to my code $scope.date = '06:31:04'; <p ng-bind="date | date:"h:mma"></p> still getting '06:31:04'
thank you, yes it work with a new date. But i have output in this format 06:31:04, is there a way to reformat this?
0

u can use the below simple example:

$scope.date=new Date();

now in the html we have:

    <h2>
        {{ today| date:'hh:mm'  }}
    </h2>

Comments

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.