I have a page, built with AngularJS, which has a input [type = date] .
Basically, what I want is to take that date and format (ie, display to the user) using the format 'dd/MM/yyyy'
(for example: 28/05/2015
).
What is the problem?
Today, are not all browsers that support input[type=date]
. Those who support (such as Chrome and Opera) format the date input
in 'dd/MM/yyyy'
format, according to the browser settings.
In browsers that do not support (like Firefox and Safari), Angular uses the format yyyy-MM-dd
to format the date of input
. This format, which although legible, may not please the users of the application, who are accustomed to the Brazilian format.
What is the solution?
In short, I could not find any way to fix this.
Angular has no directive to set the input format. So I tried to implement a directive that would do this job for me, following some SO-EN posts:
- Angular.js and HTML5 date input value - how to get Firefox to show readable date value in a date input?
- AngularJs - Directive to modify input formatting
- How to format a date using ng-model?
The result of this was the following directive:
module.directive("input", ["$filter", function($filter) {
return {
require: "ngModel",
restrict: "E",
link: function(scope, element, attrs, ngModelController) {
if (element.attr("type") != "date") {
return;
}
if (verificaSeNavegadorNaoSuportaInputDate()) {
ngModelController.$parsers.push(function(dataEmString) {
return converteStringParaData(dataEmString);
});
ngModelController.$formatters.push(function(data) {
return formataDataParaString(data);
});
}
}
};
}]);
With this directive, I can even format the date set in the model, but I can not turn user input on a Date
object . More precisely,% w / w% of% of the NgModelController fails, since, supposedly, the value of $validator
is not a date.
To try to illustrate: using the directive above, the value of date
is equal to input
.
Does anyone have any idea how I can implement / fix this policy?