Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

Commit

Permalink
fix(datepicker): fix usage of non-standard format
Browse files Browse the repository at this point in the history
- Fix display of model in input when using a non-standard format with
  the dateparser (i.e. `M!` and `d!`)

Closes #5188
Fixes #5187
  • Loading branch information
wesleycho committed Jan 9, 2016
1 parent 6ad873f commit 428beaf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
model = dateParser.fromTimezone(model, ngModelOptions.timezone);
var dt = {
date: date,
label: dateFilter(date, format),
label: dateFilter(date, format.replace(/d!/, 'dd')).replace(/M!/, 'MM'),
selected: model && this.compare(date, model) === 0,
disabled: this.isDisabled(date),
current: this.compare(date, new Date()) === 0,
Expand Down Expand Up @@ -729,6 +729,9 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
return value;
}
scope.date = dateParser.fromTimezone(value, ngModelOptions.timezone);
dateFormat = dateFormat.replace(/M!/, 'MM')
.replace(/d!/, 'dd');

return dateFilter(scope.date, dateFormat);
});
} else {
Expand Down
24 changes: 24 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1948,6 +1948,30 @@ describe('datepicker', function() {
});
});

describe('custom format with optional leading zeroes', function() {
beforeEach(inject(function() {
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup="d!-M!-yyyy" is-open="true"><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
}));

it('to display the correct value in input', function() {
expect(inputEl.val()).toBe('30-09-2010');
});

it('updates the input when a day is clicked', function() {
clickOption(10);
expect(inputEl.val()).toBe('08-09-2010');
expect($rootScope.date).toEqual(new Date('September 8, 2010 15:30:00'));
});

it('updates the input correctly when model changes', function() {
$rootScope.date = new Date('December 25, 1983 10:00:00');
$rootScope.$digest();
expect(inputEl.val()).toBe('25-12-1983');
});
});

describe('dynamic custom format', function() {
beforeEach(inject(function() {
$rootScope.format = 'dd-MMMM-yyyy';
Expand Down

0 comments on commit 428beaf

Please sign in to comment.