AngularJS 1.3 adds to input [date | datetime-local ] fields a new formatter/parser that checks that the model is a Date Object and throw the datefmt exception.
if (value && !isDate(value)) {
throw $ngModelMinErr('datefmt', 'Expected `{0}` to be a date', value);
This issue is not present at previos versions and I suggest get in consideration on upgrade to avoid big nightmares.
A workaroud is to reset default formatters/parser with a custom dateFormat directive and use momentjs to format,parse strings to Date.
define(['directives/directives','momentjs'], function(directives,momentjs) {
directives.directive('dateFormat', function() {
return {
require: 'ngModel',
link: function(scope, element, attr, ngModelCtrl) {
var format=attr.dateFormat;
//Angular 1.3 insert a formater that force to set model to date object, otherwise throw exception.
//Reset default angular formatters/parsers
ngModelCtrl.$formatters.length=0;
ngModelCtrl.$parsers.length=0;
ngModelCtrl.$formatters.push(function(valueFromModel) {
//return how data will be shown in input
if(valueFromModel){
// For momentjs > 2.9 moment global va is not defined use momentjs instead of moment.
return moment(valueFromModel).format(format);
}
else
return null;
});
ngModelCtrl.$parsers.push(function(valueFromInput) {
if(valueFromInput){
//For momentjs > 2.9 moment global va is not defined use momentjs instead of moment.
return moment(valueFromInput,format).toDate();
}
else
return null;
});
}
};
});
});
UPDATE With momentjs > 2.9 take in consideration that the moment global var are not defined.
2.9 "Deprecation warning: Accessing Moment through the global scope is deprecated, and will be removed in an upcoming release."
With AMD need use momentjs instead of moment. i.e:
return momentjs(valueFromModel).format(format);