you can use parser to parse the value before it being attached to datepicker
csapp.directive("csDateConverter", function () {
var linkFunction = function (scope, element, attrs, ngModelCtrl) {
ngModelCtrl.$parsers.push(function (datepickerValue) {
//convert the date as per your specified format
var date = moment(datepickerValue, scope.format)
//convert it to the format recognized by datepicker
return date.format("YYYY-MM-DD");
});
};
return {
scope: {format: '='}
restrict: "A",
require: "ngModel",
link: linkFunction
};
});
and you can use it like this
<datepicker ng-model="dt" cs-date-converter="yyyy.mm.dd"></datepicker>
EDIT as per comments
removed isolate scope & changed scope.format to attrs.format
csapp.directive("csDateConverter", function () {
var linkFunction = function (scope, element, attrs, ngModelCtrl) {
ngModelCtrl.$parsers.push(function (datepickerValue) {
//convert the date as per your specified format
var date = moment(datepickerValue, attrs.format.toUpperCase())
//convert it to the format recognized by datepicker
return date.format("YYYY-MM-DD");
});
};
return {
restrict: "A",
require: "ngModel",
link: linkFunction
};
});