The dateChange event returns a date, not a string. If you want to display a string with the local date, use the .toLocaleDateString()
method.
angular.module('app').directive('datepicker', function() {
return {
restrict: 'A',
require : 'ngModel',
link: function(scope, element, attrs, ngModelCtrl) {
$(element).datepicker({
dateFormat:'dd-mm-yyyy',
language: 'pt-BR',
pickTime: false,
startDate: '01-11-2013',
endDate: '01-11-2030'
}).on('changeDate', function(e) {
ngModelCtrl.$setViewValue(e.date.toLocaleDateString());
scope.$apply();
});
}
};
});
Note that toLacaleDateString()
will print the date using the client format, in our case 'dd / MM / yyyy', but if a North American client accesses your page it will look in the format 'M / d / yyyy ', in which case you can force to display in a specific format.
var data = new Date();
console.log(data.toLocaleDateString());
console.log(data.toLocaleDateString("pt-BR"));
console.log(data.toLocaleDateString("pt-PT"));
console.log(data.toLocaleDateString("en-US"));
You can also set options to change how the date is displayed:
var data = new Date();
var options = {};
options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }
console.log(data.toLocaleDateString([], options));
options = { timeZone: 'UTC', timeZoneName: 'short' }
console.log(data.toLocaleDateString([], options));
And an example of curiosity, changing the calendaio and the numerical system of the date:
var sistemas = ["arab", "arabext", "bali", "beng", "deva", "fullwide", "gujr", "guru", "hanidec", "khmr", "knda", "laoo", "latn", "limb", "mlym", "mong", "mymr", "orya", "tamldec", "telu", "thai", "tibt"];
var calendarios = ["buddhist", "chinese", "coptic", "ethioaa", "ethiopic", "gregory", "hebrew", "indian", "islamic", "islamicc", "iso8601", "japanese", "persian", "roc"]
var data = new Date();
var datas = sistemas.map(function (sistema) {
return {
sistema: sistema,
calendarios: calendarios.map(function (calendario) {
var format = "pt-BR-u-nu-" + sistema + "-ca-" + calendario;
try {
return {
calendario: calendario,
format: format,
exemplo: data.toLocaleDateString(format)
}
} catch (e) {
}
})
}
});
var srce = document.getElementById("tmpl");
var tmpl = Handlebars.compile(srce.innerHTML);
document.body.innerHTML = tmpl(datas);
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.1/handlebars.js"></script><scriptid="tmpl" type="text/template" >
<table>
<thead>
<tr>
<th></th>
{{#each this.[0].calendarios}}
<th>{{calendario}}</th>
{{/each}}
</tr>
</thead>
<tbody>
{{#each this}}
<tr>
<th>{{sistema}}</th>
{{#each calendarios}}
<td><a href="#" title="{{format}}">{{exemplo}}<a></td>
{{/each}}
</tr>
{{/each}}
</tbody>
<table>
</script>
To see the format used, simply hover over the table.
If you want everyone on the globe to see the date in dd/MM/yyyy
format, use .toLocaleDateString('pt-BR-u-nu-latn-ca-gregory')
.