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

Commit 428beaf

Browse files
committed
fix(datepicker): fix usage of non-standard format
- Fix display of model in input when using a non-standard format with the dateparser (i.e. `M!` and `d!`) Closes #5188 Fixes #5187
1 parent 6ad873f commit 428beaf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

Diff for: src/datepicker/datepicker.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
145145
model = dateParser.fromTimezone(model, ngModelOptions.timezone);
146146
var dt = {
147147
date: date,
148-
label: dateFilter(date, format),
148+
label: dateFilter(date, format.replace(/d!/, 'dd')).replace(/M!/, 'MM'),
149149
selected: model && this.compare(date, model) === 0,
150150
disabled: this.isDisabled(date),
151151
current: this.compare(date, new Date()) === 0,
@@ -729,6 +729,9 @@ function(scope, element, attrs, $compile, $parse, $document, $rootScope, $positi
729729
return value;
730730
}
731731
scope.date = dateParser.fromTimezone(value, ngModelOptions.timezone);
732+
dateFormat = dateFormat.replace(/M!/, 'MM')
733+
.replace(/d!/, 'dd');
734+
732735
return dateFilter(scope.date, dateFormat);
733736
});
734737
} else {

Diff for: src/datepicker/test/datepicker.spec.js

+24
Original file line numberDiff line numberDiff line change
@@ -1948,6 +1948,30 @@ describe('datepicker', function() {
19481948
});
19491949
});
19501950

1951+
describe('custom format with optional leading zeroes', function() {
1952+
beforeEach(inject(function() {
1953+
var wrapElement = $compile('<div><input ng-model="date" uib-datepicker-popup="d!-M!-yyyy" is-open="true"><div>')($rootScope);
1954+
$rootScope.$digest();
1955+
assignElements(wrapElement);
1956+
}));
1957+
1958+
it('to display the correct value in input', function() {
1959+
expect(inputEl.val()).toBe('30-09-2010');
1960+
});
1961+
1962+
it('updates the input when a day is clicked', function() {
1963+
clickOption(10);
1964+
expect(inputEl.val()).toBe('08-09-2010');
1965+
expect($rootScope.date).toEqual(new Date('September 8, 2010 15:30:00'));
1966+
});
1967+
1968+
it('updates the input correctly when model changes', function() {
1969+
$rootScope.date = new Date('December 25, 1983 10:00:00');
1970+
$rootScope.$digest();
1971+
expect(inputEl.val()).toBe('25-12-1983');
1972+
});
1973+
});
1974+
19511975
describe('dynamic custom format', function() {
19521976
beforeEach(inject(function() {
19531977
$rootScope.format = 'dd-MMMM-yyyy';

0 commit comments

Comments
 (0)