Response adapted from: how-to-format-a-date-in-format-dd-mm-yyyy
Use the Angularjs filters
To format dates in Angular, there is the Angular Filter which is very useful and easy to use, whether in the view
or us controllers
. The filter
can still be used to format any type of object. For date there are several possible combinations.
How to use Filters?
In javascript code (Directives, controllers, etc)
$filter('filter')(array, expression, comparator)
How to format hours?
You can convert your Date type object into any string
based on settings, see some examples:
HH
two-digit hour (00-23) - 24 hours
H
hour with one digit (0-23) - 24 hours
hh
hour with two digits (01-12) - 12 hours
h
hour with one digit (1-12) - 12 hours
mm
two-digit minutes (00-59)
m
minutes with one digit (0-59)
ss
two-digit seconds (00-59)
s
seconds with one digit (0-59)
- see the full list on angular documentation
How to apply this to your input?
You must inject the service into your controller filter
angular, and then format the value with the format you need.
angular
.module('myApp', [])
.controller('myController', myController);
myController.$inject = ['$scope', '$filter'];
function myController($scope, $filter) {
var dataHora = new Date();
$scope.hora = dataHora;
$scope.horaFormatada = $filter('date')(dataHora, 'HH:mm:ss');
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<label>Hora sem formato</label><br />
<input type="time" ng-model="hora" /><br />
<label>Hora formatada</label><br />
<input type="time" ng-model="horaFormatada" />
</div>
Which version of the angle you are using?
– Sorack
You need to use the Angular filters, see in that reply I explain how to do it. In your case you will need to use the format
HH:mm:ss
.– Pedro Camara Junior
See on documentation (in English) all formats you can use.
– Pedro Camara Junior
@miltoncamara is not duplicate. He wants to format the hour within a
input
. Plus it’s not a full date.– Sorack
@miltoncamara I had looked at this post, but in my case I would have to format this time in an input, I tested the shape of this post but only worked with label, what is not my case rs
– Brayan