Skip to content

Instantly share code, notes, and snippets.

@yannvery
Created May 6, 2014 10:45
Show Gist options
  • Select an option

  • Save yannvery/b2d8b551d78e6c05dea9 to your computer and use it in GitHub Desktop.

Select an option

Save yannvery/b2d8b551d78e6c05dea9 to your computer and use it in GitHub Desktop.
Angular Directive that accept to enter a EU date on angular-ui Datepicker
app.directive('dateEu', [ 'datepickerPopupConfig', function( datepickerPopupConfig) {
// return the directive link function. (compile function not needed)
return {
restrict: 'EA',
require: 'ngModel', // get a hold of NgModelController
link: function(scope, element, attrs, ngModel) {
var format = attrs.datepickerPopup;
var maxDate = scope[attrs.max];
var minDate = scope[attrs.min];
var model = ngModel;
ngModel.$parsers.push(function(viewValue) {
var newDate = model.$viewValue;
var date = null;
// pass through if we clicked date from popup
if (typeof newDate === "object" || newDate == "") return newDate;
// build a new date according to initial localized date format
if (format === "dd/MM/yyyy") {
// extract day, month and year
var splitted = newDate.split('/');
var month = parseInt(splitted[1]) - 1;
date = new Date(splitted[2], month, splitted[0]);
// if maxDate,minDate is set make sure we do not allow greater values
if (maxDate && date > maxDate) date = maxDate;
if (minDate && date < minDate) date = minDate;
model.$setValidity('date', true);
model.$setViewValue(date);
}
return date ? date : viewValue;
})
}
}
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment